2014-06-21 14 views
10

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?

+1

Cercavi 'nice_type' invece di' cool_type' in linea 8? –

+0

@RSahu si, mi dispiace. – sbabbi

+0

A cosa serve? –

risposta

9

Questo è il bug gcc 57891. La conversione della costante integrale 42 a bool implica una conversione di riduzione, che non è consentita negli argomenti modello non di tipo. Quindi lo enable_if non è ben formato e la specializzazione deve essere eliminata, come fa correttamente Clang.

§14.3.2/5 [temp.arg.nontype]

The following conversions are performed on each expression used as a non-type template-argument. If a non-type template-argument cannot be converted to the type of the corresponding template-parameter then the program is ill-formed.
— For a non-type template-parameter of integral or enumeration type, conversions permitted in a converted constant expression (5.19) are applied.
...

§5.19/3 [expr.const]

... A converted constant expression of type T is an expression, implicitly converted to a prvalue of type T , where the converted expression is a core constant expression and the implicit conversion sequence contains only user-defined conversions, lvalue-to-rvalue conversions (4.1), integral promotions (4.5), and integral conversions (4.7) other than narrowing conversions (8.5.4).

§8.5.4/7 [dcl.init.list]

A narrowing conversion is an implicit conversion
...
— from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.


Questa minimal example dimostra il bug gcc:

template<bool> 
struct foo{}; 
foo<10> f; 

int main() {} 

gcc-4.9 accetta il codice mentre clang-3.4 respinge con il seguente errore:

error: non-type template argument evaluates to 10, which cannot be narrowed to type 'bool' [-Wc++11-narrowing]

foo<10> f; 
    ^

La correzione per il vostro particolare il problema è facile. Assicurarsi che il non-tipo di modello argomento per enable_if restituisce un bool

template<class T> 
struct pick<T, typename std::enable_if< is_nice<T>::value != 0 >::type > 
//              ^^^^^^ 
{ 
    typedef std::integral_constant<int, is_nice<T>::value > type; 
}; 
Problemi correlati