2014-06-21 3 views
10

betrachte das folgende Beispiel:Narrowing int in SFINAE, verschiedene Ausgangs zwischen gcc und Klappern in bool

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) gibt "-1, -1", während GCC (4.9.0) Ausgänge "-1, 42".

Das Problem liegt in der Spezialisierung von . Während Gcc scheint, is_nice<T>::value (42) zu bool(true) zu konvertieren, tut clang das nicht und verwirft die Spezialisierung. Beide Beispiele sind zusammengestellt mit -std=c++11.

Welcher Compiler ist richtig?

+1

wertet Meinen Sie 'nice_type' statt' cool_type' in Zeile 8? –

+0

@RSahu ja, tut mir leid. – sbabbi

+0

Wofür ist das? –

Antwort

9

Dies ist gcc bug 57891. Die Umwandlung der Integralkonstanten 42 in bool beinhaltet eine einschränkende Konvertierung, die in Nicht-Typ-Template-Argumenten nicht erlaubt ist. Daher ist die enable_if schlecht gebildet, und die Spezialisierung sollte verworfen werden, wie es richtig kling macht.

§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.


Diese minimal example zeigt die gcc Fehler:

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

int main() {} 

gcc-4.9 übernimmt den Code während Klirren-3.4 mit dem folgenden Fehler ablehnt:

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

foo<10> f; 
    ^

das Update zu Ihrem speziellen Problem ist einfach. Stellen Sie sicher, dass das nicht-Typ Template-Argument zu enable_if zu einem 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; 
};