consideri il seguente esempio:restringimento int a bool in SFINAE, output diverso tra gcc e clang
template<int i>
struct nice_type;
template<class T>
struct is_nice : std::false_type {};
template<int i>
struct is_nice< nice_type<i> > : std::integral_constant<int, i> {};
template<class T, class = void>
struct pick
{
typedef std::integral_constant<int, -1> type;
};
template<class T>
struct pick<T, typename std::enable_if< is_nice<T>::value >::type >
{
typedef std::integral_constant<int, is_nice<T>::value > type;
};
int main()
{
std::cout << pick<int>::type::value << ", ";
std::cout << pick< nice_type<42> >::type::value << std::endl;
return 0;
}
Clang (3.4.1) uscite "-1, -1", mentre GCC (4.9.0) uscite "-1, 42".
Il problema risiede nella specializzazione di pick
. Mentre Gcc sembra felice di convertire is_nice<T>::value
(42) in bool(true)
, clang non lo fa, e scarta la specializzazione. Entrambi gli esempi sono stati compilati con -std=c++11
.
Quale compilatore ha ragione?
Cercavi 'nice_type' invece di' cool_type' in linea 8? –
@RSahu si, mi dispiace. – sbabbi
A cosa serve? –