作者yoco315 (眠月)
看板C_and_CPP
標題Re: [問題] 限制 Template 的具現化
時間Sun Apr 29 13:16:13 2012
※ 引述《birdhackor (夜殘狼)》之銘言:
: 有沒有辦法指定他只能具現化特定型別的函數呢?
#include <type_traits>
class Base {} ;
class Derived {} ;
template < typename T >
T operator+(
const T& lhs,
const typename
std::enable_if<
std::is_base_of<Base, T>::value,
T
>::type& rhs)
{
...
}
這樣就只有 Base 跟繼承他 Base 的 class 會套用這個 operator+。
因為你剛開始學 template,細節就先不說了,先把他當作一個 magic 就好了 /_\
--
To iterate is human, to recurse, divine.
遞迴只應天上有, 凡人該當用迴圈. L. Peter Deutsch
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 182.235.170.158
→ birdhackor:我來試看看~謝謝 04/29 15:47
推 loveme00835:magic XD 04/29 16:30
推 holymars:wow~~~magic!! 04/29 19:00
推 damody:太神奇了!傑克! 04/30 09:58
→ yoco315:loveme00835 holymars 對兩位來說只是基本技能 04/30 23:55