Expected behavior of template parameters out of range?

template<bool b = 2> void foo(void) {} template void foo(); template<unsigned char n = 258> void bar(void) {} template void bar(); 

GCC creates an instance of foo <true> and bar <2>; Klang rejects both arguments "error: non-type template" equal to 2, which cannot be narrowed to type "bool" [-WC ++ 11-narrowing] ".

Is the code above valid? Is this a mistake in one of them?

Used versions: Clang 3.8.0-2ubuntu4, GCC 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.2)

+5
source share
1 answer

This is gcc error 57891 and 60715 .

From [temp.arg.nontype]:

The template argument for the non-type of the template parameter must be a transformed constant expression (5.20) of the type of the template parameter.

From [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 sequence of the implicit conversion contains only [...] integral transforms (4.7), except for narrowing conversions (8.5.4),

From [dcl.init.list]:

A narrowing of a transformation is an implicit conversion [...] from an integer or non-enumerated type to an integer type that cannot represent all the values โ€‹โ€‹of the original type, except when the source is a constant expression, the value of which after the integral, the promotions will fit into target type.

Conversion narrowing (for example, from 2 to bool or from 258 to char ) is poorly formed for non-standard template parameters.

+7
source

All Articles