2016-08-15 1 views
5
template<bool b = 2> void foo(void) {} 
template void foo(); 

template<unsigned char n = 258> void bar(void) {} 
template void bar(); 

GCC instanziiert foo < true> und bar < 2>; Clang lehnt beides ab mit "error: non-type template argument ergibt 2, das nicht eingeschränkt werden kann, um 'bool' [-WC++ 11-enning]" einzugeben.Erwartetes Verhalten bei Vorlagenparametern außerhalb des Bereichs?

Ist der obige Code gültig? Ist das ein Fehler in einem von ihnen?

verwendet Versionen: Clang 3.8.0-2ubuntu4, 5.4.0 GCC 20.160.609 (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.2)

Antwort

7

Dieser gcc Fehler ist 57891 und 60715.

Von [temp.arg.nontype]:

A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter.

Von [expr.const]:

A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only [...] integral conversions (4.7) other than narrowing conversions (8.5.4),

Von [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.

Einschränkende Umrechnungen (zB 2 bis bool oder 258 bis char) sind ungünstig d für Template-Nicht-Typparameter.

Verwandte Themen