5
To jest moja pierwsza próba SFINAE:Nie można uzyskać SFINAE pracować
#include <type_traits>
#include <iostream>
struct C1 {
using T = int;
};
struct C2 {
using T = void;
};
// For classes that declare T = int
template <class C>
void f(C &c,
std::enable_if<!std::is_same<typename C::T, void>::value, int>::type = 0) {
std::cout << "With T" << std::endl;
}
// For classes that declare T = void
template <class C>
void f(C &c,
std::enable_if<std::is_same<typename C::T, void>::value, int>::type = 0) {
std::cout << "Without T" << std::endl;
}
int main() {
C1 c1;
f(c1); // With T
C2 c2;
f(c2); // Without T
return 0;
}
kompilator (gcc 4.8.2) narzeka:
‘std::enable_if<!(std::is_same<typename C::T, void>::value), int>::type’ is not a type
Co robię źle?
Musisz napisać 'TypeName std :: enable_if <...> :: type' ponieważ 'type' jest zależny. [Przeczytaj to] (https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords). Jeśli możesz użyć C++ 14, możesz zamiast tego użyć 'std :: enable_if_t <...>' i nie będziesz miał tego problemu. – Simple
Najlepiej byłoby umieścić 'enable_if's w parametrach szablonu, aby nie zaszumieć argumentów funkcji –
Zrobiłem edycję twojego kodu. Zakładam, że były to rzeczy, które po prostu pominąłeś lub nieprawidłowo wpisałeś, ale dodałem je dla jasności. – erip