Why this does not compile with gcc48 and clang32?
#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 error:
/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 error:
/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;
^
EDIT - SOLUTION
I've accepted answer from Charles Salvia, but for practical reasons I was not able to use workaround that was proposed (specialize on N). I found other workaround which works for me. Make enable_if
depend on T
:
typename std::enable_if<(sizeof(T),N==1), int>::type
enable_if
is implemented as a language construct rather than by, as I am currently assuming, a simple templated class). – zneakerror: no matching function for call ...
– Li Dongsizeof(T)
does not work in my case. Sorry about the downvote. I was just frustrated then. I used default template argument in C++11. – Li Dong