Зачем компилировать ошибку с помощью enable_if
Почему это не компилируется с помощью gcc48 и 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:
/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
Ответы
Ответ 1
Потому что вы используете enable_if
без использования параметра шаблона T
в своих шаблонах функций. Если вы хотите специализироваться, когда struct S
имеет определенное значение параметра шаблона N
, вам нужно будет использовать специализацию класса.
template <int N, class Enable = void>
struct S { };
template <int N>
struct S<N, typename std::enable_if<N == 1>::type>
{
....
};
Ответ 2
Ну, я не уверен, что вы хотели сделать, но, возможно, этот код поможет:
#include <iostream>
template <int N>
struct S {
template<class T=int>
typename std::enable_if<N==1, T>::type
f(T t) {return 1;}
template<class T=int>
typename std::enable_if<N!=1, T>::type
f(T t) {return 2;}
};
int main()
{
S<1> s1;
S<2> s2;
std::cout << s1.f(99) << " " << std::endl << s2.f(5);
}
Отпечатки 1 и 2.
Ответ 3
Чтобы заставить std::enable_if
работать так, вы полагаетесь на SFINAE. К сожалению, в момент, когда вы объявляете
S<1> s1;
он создаст экземпляр всех объявлений S<1>
. SFINAE войдет в игру только в этом случае, если S<1>
была плохо сформированной конструкцией. Это не. К сожалению, он содержит недопустимую функцию, поэтому создание S<>
недействительно.
Для таких вещей я могу отложить до отдельной структуры шаблона:
template <bool B>
struct f_functor {
template <typename T>
static int f(T t) { return 1; }
};
template <>
struct f_functor<false> {
template <typename T>
static int f(T t) { return 2; }
};
template <int N>
struct S {
template<class T>
typename int f(T t) { return f_functor<N==1>::f(t); }
};
Ответ 4
Используйте параметр булевого шаблона по умолчанию, например:
template <int N>
struct S {
template<class T, bool EnableBool=true>
typename std::enable_if<N==1 && EnableBool, int>::type
f(T t) {return 1;};
template<class T, bool EnableBool=true>
typename std::enable_if<N!=1 && EnableBool, int>::type
f(T t) {return 2;};
};
Ответ 5
В этом случае вы можете думать о том, что вообще не используете enable_if. Можно просто специализировать f:
template <int N>
struct S {
template<class T> int f(T t);
};
template<int N>
template<class T>
int S<N>::f(T t) { return 2; }
template<>
template<class T>
int S<1>::f(T t) { return 1; }
int main() {
S<1> s1;
return s1.f(99);
}