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

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

GCC instantiates foo < true> và bar < 2>; Clang từ chối cả hai với "lỗi: đối số mẫu không đánh giá là 2, không thể thu hẹp để nhập 'bool' [-WC++ 11-narrowing]".Hành vi mong đợi trên các tham số mẫu nằm ngoài phạm vi?

Mã trên có hợp lệ không? Đây có phải là lỗi trong một trong số đó không?

phiên bản sử dụng: Clang 3.8.0-2ubuntu4, GCC 5.4.0 20.160.609 (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.2)

Trả lời

7

Đây là gcc lỗi 5789160715.

Từ [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.

Từ [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),

Từ [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.

Thu hẹp các chuyển đổi (ví dụ: 2 đến bool hoặc 258 đến char) bị lỗi d cho các tham số không kiểu mẫu.

Các vấn đề liên quan