爲什麼這個編譯:爲什麼我的模板不能重載?
class Testable {
public:
template <bool flag>
typename std::enable_if<flag>::type
void foo() { cout << "Yay" << endl; }
template <bool flag>
typename std::enable_if<!flag>::type
void foo() { cout << "Nay" << endl; }
};
但如果我定義使用的默認類型,像這樣既FOOS:
template <bool flag, typename = typename std::enable_if<flag>::type>
void foo() { cout << "Yay" << endl; } // (A)
template <bool flag, typename = typename std::enable_if<!flag>::type>
void foo() { cout << "Nay" << endl; } // (B)
我得到這個錯誤(第一線指向的(B)
定義,第二個指向(A)
):
error: 'template<bool flag, class> void Testable::foo()' cannot be overloaded
error: with 'template<bool flag, class>> void Testable::foo()'
這實際上仍然不能編譯我(在gcc4.6.3上)。當我嘗試調用foo()時,我得到'沒有與'Testable :: foo()'調用匹配的函數。 –
Barry
2013-04-26 18:03:28
@Barry你必須有'foo'模板參數。沒有什麼可以爲你推演的。你試過'obj.foo();'? –
WhozCraig
2013-04-26 18:04:22
@WhozCraig - 是的,我的整個主要只是'Testable t; t.foo();' –
Barry
2013-04-26 18:06:17