Dlaczego to nie kompiluje się z gcc48 i clang32? BłądDlaczego warto skompilować błąd za pomocą 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: 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;};
^
dzyń:
/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 - ROZWIĄZANIE
Mam akceptowane odpowiedź od Karola Salvia, ale ze względów praktycznych ja nie był w stanie zastosować proponowanego obejścia (specjalizacja na N). Znalazłem inne obejście, które działa dla mnie. Dokonaj enable_if
zależą T
:
typename std::enable_if<(sizeof(T),N==1), int>::type
Czy jesteś pewien, że to będzie działać tak czy inaczej? Nie można przeciążać metod po prostu przez ich typ zwracany (chyba że 'enable_if' jest zaimplementowany jako konstrukcja językowa, a nie przez, jak się obecnie domyślam, prosta klasa szablonów). – zneak
Dlaczego obejście problemu polegające na tym, że włączenie opcji enable_if zależy dokładnie od T, dokładnie? –
Rozwiązanie nie działa dla mnie! Dostałem 'error: no matching function for call ...' –