http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60715
Bug ID: 60715 Summary: Narrowing conversions not caught in non-type template parameters Product: gcc Version: 4.9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: filip.roseen at gmail dot com Created attachment 32493 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=32493&action=edit testcase.cpp template< char> void f () {} template<unsigned int> void g () {} template<unsigned int> struct A {}; int main () { f<1024> (); // ill-formed, `char { 1024 }` is a narrowing conversion, // see [dcl.init.list]p7 g<-123> (); // ill-formed, `unsigned int { -123 }` is a narrowing // conversion, see [dcl.init.list]p7 A<-123> a; // ill-formed, see previous comment } /* In the above we assume `char` is 8 bits */ ------------------------------------------------------------------------- [temp.arg.nontype]p1; > A template-argument for a non-type, non-template template > parameeter shall be one of: > > - for a non-type template-aprameter of integral or enumeration > type, a converted constant expression (5.19) of the type of > the template-parameter > > <snip /> [expr.const]p3; > 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). ------------------------------------------------------------------------- [ Note: `gcc` accepts testcase.cpp, while `clang` (correctly) issues the relevant diagnostics. ] [ Note: `gcc` handles overflow conversions for non-type template parameters when used in a class template; ie. it shows a diagnostic for `template<char> struct B {};` instantiated as `B<1024> {};` ]