Home | History | Annotate | Download | only in SemaTemplate
      1 // RUN: %clang_cc1 -fsyntax-only -std=c++98 -Wconversion -verify %s
      2 template<int N> struct A; // expected-note 5{{template parameter is declared here}}
      3 
      4 A<0> *a0;
      5 
      6 A<int()> *a1; // expected-error{{template argument for non-type template parameter is treated as function type 'int ()'}}
      7 
      8 A<int> *a2; // expected-error{{template argument for non-type template parameter must be an expression}}
      9 
     10 A<1 >> 2> *a3; // expected-warning{{use of right-shift operator ('>>') in template argument will require parentheses in C++11}}
     11 
     12 // C++ [temp.arg.nontype]p5:
     13 A<A> *a4; // expected-error{{must be an expression}}
     14 
     15 enum E { Enumerator = 17 };
     16 A<E> *a5; // expected-error{{template argument for non-type template parameter must be an expression}}
     17 template<E Value> struct A1; // expected-note{{template parameter is declared here}}
     18 A1<Enumerator> *a6; // okay
     19 A1<17> *a7; // expected-error{{non-type template argument of type 'int' cannot be converted to a value of type 'E'}}
     20 
     21 const long LongValue = 12345678;
     22 A<LongValue> *a8;
     23 const short ShortValue = 17;
     24 A<ShortValue> *a9;
     25 
     26 int f(int);
     27 A<f(17)> *a10; // expected-error{{non-type template argument of type 'int' is not an integral constant expression}}
     28 
     29 class X {
     30 public:
     31   X();
     32   X(int, int);
     33   operator int() const;
     34 };
     35 A<X(17, 42)> *a11; // expected-error{{non-type template argument of type 'X' must have an integral or enumeration type}}
     36 
     37 float f(float);
     38 
     39 float g(float); // expected-note 2{{candidate function}}
     40 double g(double); // expected-note 2{{candidate function}}
     41 
     42 int h(int);
     43 float h2(float);
     44 
     45 template<int fp(int)> struct A3; // expected-note 1{{template parameter is declared here}}
     46 A3<h> *a14_1;
     47 A3<&h> *a14_2;
     48 A3<f> *a14_3;
     49 A3<&f> *a14_4;
     50 A3<h2> *a14_6;  // expected-error{{non-type template argument of type 'float (float)' cannot be converted to a value of type 'int (*)(int)'}}
     51 A3<g> *a14_7; // expected-error{{address of overloaded function 'g' does not match required type 'int (int)'}}
     52 
     53 
     54 struct Y { } y;
     55 
     56 volatile X * X_volatile_ptr;
     57 template<X const &AnX> struct A4; // expected-note 2{{template parameter is declared here}}
     58 X an_X;
     59 A4<an_X> *a15_1; // okay
     60 A4<*X_volatile_ptr> *a15_2; // expected-error{{non-type template argument does not refer to any declaration}}
     61 A4<y> *15_3; //  expected-error{{non-type template parameter of reference type 'const X &' cannot bind to template argument of type 'struct Y'}} \
     62             // FIXME: expected-error{{expected unqualified-id}}
     63 
     64 template<int (&fr)(int)> struct A5; // expected-note{{template parameter is declared here}}
     65 A5<h> *a16_1;
     66 A5<f> *a16_3;
     67 A5<h2> *a16_6;  // expected-error{{non-type template parameter of reference type 'int (&)(int)' cannot bind to template argument of type 'float (float)'}}
     68 A5<g> *a14_7; // expected-error{{address of overloaded function 'g' does not match required type 'int (int)'}}
     69 
     70 struct Z {
     71   int foo(int);
     72   float bar(float);
     73   int bar(int);
     74   double baz(double);
     75 
     76   int int_member;
     77   float float_member;
     78 };
     79 template<int (Z::*pmf)(int)> struct A6; // expected-note{{template parameter is declared here}}
     80 A6<&Z::foo> *a17_1;
     81 A6<&Z::bar> *a17_2;
     82 A6<&Z::baz> *a17_3; // expected-error{{non-type template argument of type 'double (Z::*)(double)' cannot be converted to a value of type 'int (Z::*)(int)'}}
     83 
     84 
     85 template<int Z::*pm> struct A7;  // expected-note{{template parameter is declared here}}
     86 template<int Z::*pm> struct A7c;
     87 A7<&Z::int_member> *a18_1;
     88 A7c<&Z::int_member> *a18_2;
     89 A7<&Z::float_member> *a18_3; // expected-error{{non-type template argument of type 'float Z::*' cannot be converted to a value of type 'int Z::*'}}
     90 A7c<(&Z::int_member)> *a18_4; // expected-warning{{address non-type template argument cannot be surrounded by parentheses}}
     91 
     92 template<unsigned char C> struct Overflow; // expected-note{{template parameter is declared here}}
     93 
     94 Overflow<5> *overflow1; // okay
     95 Overflow<255> *overflow2; // okay
     96 Overflow<256> *overflow3; // expected-warning{{non-type template argument value '256' truncated to '0' for template parameter of type 'unsigned char'}}
     97 
     98 
     99 template<unsigned> struct Signedness; // expected-note{{template parameter is declared here}}
    100 Signedness<10> *signedness1; // okay
    101 Signedness<-10> *signedness2; // expected-warning{{non-type template argument with value '-10' converted to '4294967286' for unsigned template parameter of type 'unsigned int'}}
    102 
    103 template<signed char C> struct SignedOverflow; // expected-note 3 {{template parameter is declared here}}
    104 SignedOverflow<1> *signedoverflow1;
    105 SignedOverflow<-1> *signedoverflow2;
    106 SignedOverflow<-128> *signedoverflow3;
    107 SignedOverflow<-129> *signedoverflow4; // expected-warning{{non-type template argument value '-129' truncated to '127' for template parameter of type 'signed char'}}
    108 SignedOverflow<127> *signedoverflow5;
    109 SignedOverflow<128> *signedoverflow6; // expected-warning{{non-type template argument value '128' truncated to '-128' for template parameter of type 'signed char'}}
    110 SignedOverflow<(unsigned char)128> *signedoverflow7; // expected-warning{{non-type template argument value '128' truncated to '-128' for template parameter of type 'signed char'}}
    111 
    112 // Check canonicalization of template arguments.
    113 template<int (*)(int, int)> struct FuncPtr0;
    114 int func0(int, int);
    115 extern FuncPtr0<&func0> *fp0;
    116 template<int (*)(int, int)> struct FuncPtr0;
    117 extern FuncPtr0<&func0> *fp0;
    118 int func0(int, int);
    119 extern FuncPtr0<&func0> *fp0;
    120 
    121 // PR5350
    122 namespace ns {
    123   template <typename T>
    124   struct Foo {
    125     static const bool value = true;
    126   };
    127 
    128   template <bool b>
    129   struct Bar {};
    130 
    131   const bool value = false;
    132 
    133   Bar<bool(ns::Foo<int>::value)> x;
    134 }
    135 
    136 // PR5349
    137 namespace ns {
    138   enum E { k };
    139 
    140   template <E e>
    141   struct Baz  {};
    142 
    143   Baz<k> f1;  // This works.
    144   Baz<E(0)> f2;  // This too.
    145   Baz<static_cast<E>(0)> f3;  // And this.
    146 
    147   Baz<ns::E(0)> b1;  // This doesn't work.
    148   Baz<static_cast<ns::E>(0)> b2;  // This neither.
    149 }
    150 
    151 // PR5597
    152 template<int (*)(float)> struct X0 { };
    153 
    154 struct X1 {
    155     static int pfunc(float);
    156 };
    157 void test_X0_X1() {
    158   X0<X1::pfunc> x01;
    159 }
    160 
    161 // PR6249
    162 namespace pr6249 {
    163   template<typename T, T (*func)()> T f() {
    164     return func();
    165   }
    166 
    167   int h();
    168   template int f<int, h>();
    169 }
    170 
    171 namespace PR6723 {
    172   template<unsigned char C> void f(int (&a)[C]); // expected-note {{candidate template ignored}} \
    173   // expected-note{{substitution failure [with C = '\x00']}}
    174   void g() {
    175     int arr512[512];
    176     f(arr512); // expected-error{{no matching function for call}}
    177     f<512>(arr512); // expected-error{{no matching function for call}}
    178   }
    179 }
    180 
    181 // Check that we instantiate declarations whose addresses are taken
    182 // for non-type template arguments.
    183 namespace EntityReferenced {
    184   template<typename T, void (*)(T)> struct X { };
    185 
    186   template<typename T>
    187   struct Y {
    188     static void f(T x) {
    189       x = 1; // expected-error{{assigning to 'int *' from incompatible type 'int'}}
    190     }
    191   };
    192 
    193   void g() {
    194     typedef X<int*, Y<int*>::f> x; // expected-note{{in instantiation of}}
    195   }
    196 }
    197 
    198 namespace PR6964 {
    199   template <typename ,int, int = 9223372036854775807L > // expected-warning 2{{non-type template argument value '9223372036854775807' truncated to '-1' for template parameter of type 'int'}} \
    200   // expected-note 2{{template parameter is declared here}}
    201   struct as_nview { };
    202 
    203   template <typename Sequence, int I0>
    204   struct as_nview<Sequence, I0>  // expected-note{{while checking a default template argument used here}}
    205   { };
    206 }
    207 
    208 // rdar://problem/8302138
    209 namespace test8 {
    210   template <int* ip> struct A {
    211     int* p;
    212     A() : p(ip) {}
    213   };
    214 
    215   void test0() {
    216     extern int i00;
    217     A<&i00> a00;
    218   }
    219 
    220   extern int i01;
    221   void test1() {
    222     A<&i01> a01;
    223   }
    224 
    225 
    226   struct C {
    227     int x;
    228     char y;
    229     double z;
    230   };
    231 
    232   template <C* cp> struct B {
    233     C* p;
    234     B() : p(cp) {}
    235   };
    236 
    237   void test2() {
    238     extern C c02;
    239     B<&c02> b02;
    240   }
    241 
    242   extern C c03;
    243   void test3() {
    244     B<&c03> b03;
    245   }
    246 }
    247 
    248 namespace PR8372 {
    249   template <int I> void foo() { } // expected-note{{template parameter is declared here}}
    250   void bar() { foo <0x80000000> (); } // expected-warning{{non-type template argument value '2147483648' truncated to '-2147483648' for template parameter of type 'int'}}
    251 }
    252 
    253 namespace PR9227 {
    254   template <bool B> struct enable_if_bool { };
    255   template <> struct enable_if_bool<true> { typedef int type; };
    256   void test_bool() { enable_if_bool<false>::type i; } // expected-error{{enable_if_bool<false>}}
    257 
    258   template <char C> struct enable_if_char { };
    259   template <> struct enable_if_char<'a'> { typedef int type; };
    260   void test_char_0() { enable_if_char<0>::type i; } // expected-error{{enable_if_char<'\x00'>}}
    261   void test_char_b() { enable_if_char<'b'>::type i; } // expected-error{{enable_if_char<'b'>}}
    262   void test_char_possibly_negative() { enable_if_char<'\x02'>::type i; } // expected-error{{enable_if_char<'\x02'>}}
    263   void test_char_single_quote() { enable_if_char<'\''>::type i; } // expected-error{{enable_if_char<'\''>}}
    264   void test_char_backslash() { enable_if_char<'\\'>::type i; } // expected-error{{enable_if_char<'\\'>}}
    265 }
    266 
    267 namespace PR10579 {
    268   namespace fcppt
    269   {
    270     namespace container
    271     {
    272       namespace bitfield
    273       {
    274 
    275         template<
    276           typename Enum,
    277           Enum Size
    278           >
    279         class basic;
    280 
    281         template<
    282           typename Enum,
    283           Enum Size
    284           >
    285         class basic
    286         {
    287         public:
    288           basic()
    289           {
    290           }
    291         };
    292 
    293       }
    294     }
    295   }
    296 
    297   namespace
    298   {
    299 
    300     namespace testenum
    301     {
    302       enum type
    303         {
    304           foo,
    305           bar,
    306           size
    307         };
    308     }
    309 
    310   }
    311 
    312   int main()
    313   {
    314     typedef fcppt::container::bitfield::basic<
    315     testenum::type,
    316       testenum::size
    317       > bitfield_foo;
    318 
    319     bitfield_foo obj;
    320   }
    321 
    322 }
    323 
    324 template <int& I> struct PR10766 { static int *ip; };
    325 template <int& I> int* PR10766<I>::ip = &I;
    326 
    327 namespace rdar13000548 {
    328   template<typename R, typename U, R F>
    329   U f() { return &F; } // expected-error{{cannot take the address of an rvalue of type 'int (*)(int)'}} expected-error{{cannot take the address of an rvalue of type 'int *'}}
    330 
    331   int g(int);
    332   int y[3];
    333   void test()
    334   {
    335     f<int(int), int (*)(int), g>(); // expected-note{{in instantiation of}}
    336     f<int[3], int*, y>(); // expected-note{{in instantiation of}}
    337   }
    338 
    339 }
    340 
    341 namespace rdar13806270 {
    342   template <unsigned N> class X { };
    343   const unsigned value = 32;
    344   struct Y {
    345     X<value + 1> x;
    346   };
    347   void foo() {}
    348 }
    349