爲什麼這不與gcc48和clang32編譯?爲什麼編譯錯誤與enable_if
#include <type_traits>
template <int N>
struct S {
template<class T>
typename std::enable_if<N==1, int>::type
f(T t) {return 1;};
template<class T>
typename std::enable_if<N!=1, int>::type
f(T t) {return 2;};
};
int main() {
S<1> s1;
return s1.f(99);
}
GCC錯誤:
/home/lvv/p/sto/test/t.cc:12:2: error: no type named ‘type’ in ‘struct enable_if<false, int>’
f(T t) {return 2;};
^
CLANG錯誤:
/home/lvv/p/sto/test/t.cc:11:26: error: no type named 'type' in 'std::enable_if<false, int>'; 'enable_if' cannot be used to
disable this declaration
typename std::enable_if<N!=1, int>::type
^~~~
/home/lvv/p/sto/test/t.cc:16:7: note: in instantiation of template class 'S<1>' requested here
S<1> s1;
^
編輯 - 解決方案:
我接受的答案,從查爾斯丹蔘,但實際原因我無法使用建議的解決方法(專門針對N)。我找到了適用於我的其他解決方法。讓enable_if
取決於T
:
typename std::enable_if<(sizeof(T),N==1), int>::type
你確定這是去反正工作?你不能僅僅通過它們的返回類型來重載方法(除非'enable_if'被實現爲一種語言結構,而不是像我現在所假設的那樣實現一個簡單的模板化類)。 – zneak
爲什麼使enable_if的解決方法完全依賴於T所必需的? –
該解決方案對我無效!我得到了'錯誤:沒有匹配的調用函數...' –