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

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

GCCはfoo < true>とbar < 2>をインスタンス化します。 Clangは "error:non-typeのテンプレート引数が2に評価されますが、これはbool '[-WC++ 11-narrowing]"と入力すると縮小できません。範囲外のテンプレートパラメータで予想される動作?

上記のコードは有効ですか?このうちの1つにバグですか?使用

バージョン:クラン3.8.0-2ubuntu4、GCC 5.4.0 20160609(〜16.04.2 Ubuntuの5.4.0-6ubuntu1)

答えて

7

これはgccのバグ5789160715です。 [dcl.init.list]から

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),

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.

[expr.const]から

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

:[temp.arg.nontype]から

変換の範囲を狭める(例:2boolまたは258char)が悪いテンプレート非型パラメータの場合はdです。