1// RUN: %clang_cc1 -fsyntax-only -std=c++98 -Wconversion -verify %s
2template<int N> struct A; // expected-note 5{{template parameter is declared here}}
3
4A<0> *a0;
5
6A<int()> *a1; // expected-error{{template argument for non-type template parameter is treated as type 'int ()'}}
7
8A<int> *a2; // expected-error{{template argument for non-type template parameter must be an expression}}
9
10A<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:
13A<A> *a4; // expected-error{{must be an expression}}
14
15enum E { Enumerator = 17 };
16A<E> *a5; // expected-error{{template argument for non-type template parameter must be an expression}}
17template<E Value> struct A1; // expected-note{{template parameter is declared here}}
18A1<Enumerator> *a6; // okay
19A1<17> *a7; // expected-error{{non-type template argument of type 'int' cannot be converted to a value of type 'E'}}
20
21const long LongValue = 12345678;
22A<LongValue> *a8;
23const short ShortValue = 17;
24A<ShortValue> *a9;
25
26int f(int);
27A<f(17)> *a10; // expected-error{{non-type template argument of type 'int' is not an integral constant expression}}
28
29class X {
30public:
31  X();
32  X(int, int);
33  operator int() const;
34};
35A<X(17, 42)> *a11; // expected-error{{non-type template argument of type 'X' must have an integral or enumeration type}}
36
37float f(float);
38
39float g(float); // expected-note 2{{candidate function}}
40double g(double); // expected-note 2{{candidate function}}
41
42int h(int);
43float h2(float);
44
45template<int fp(int)> struct A3; // expected-note 1{{template parameter is declared here}}
46A3<h> *a14_1;
47A3<&h> *a14_2;
48A3<f> *a14_3;
49A3<&f> *a14_4;
50A3<h2> *a14_6;  // expected-error{{non-type template argument of type 'float (float)' cannot be converted to a value of type 'int (*)(int)'}}
51A3<g> *a14_7; // expected-error{{address of overloaded function 'g' does not match required type 'int (int)'}}
52
53
54struct Y { } y;
55
56volatile X * X_volatile_ptr;
57template<X const &AnX> struct A4; // expected-note 2{{template parameter is declared here}}
58X an_X;
59A4<an_X> *a15_1; // okay
60A4<*X_volatile_ptr> *a15_2; // expected-error{{non-type template argument does not refer to any declaration}}
61A4<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
64template<int (&fr)(int)> struct A5; // expected-note{{template parameter is declared here}}
65A5<h> *a16_1;
66A5<f> *a16_3;
67A5<h2> *a16_6;  // expected-error{{non-type template parameter of reference type 'int (&)(int)' cannot bind to template argument of type 'float (float)'}}
68A5<g> *a14_7; // expected-error{{address of overloaded function 'g' does not match required type 'int (int)'}}
69
70struct 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};
79template<int (Z::*pmf)(int)> struct A6; // expected-note{{template parameter is declared here}}
80A6<&Z::foo> *a17_1;
81A6<&Z::bar> *a17_2;
82A6<&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
85template<int Z::*pm> struct A7;  // expected-note{{template parameter is declared here}}
86template<int Z::*pm> struct A7c;
87A7<&Z::int_member> *a18_1;
88A7c<&Z::int_member> *a18_2;
89A7<&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::*'}}
90A7c<(&Z::int_member)> *a18_4; // expected-warning{{address non-type template argument cannot be surrounded by parentheses}}
91
92template<unsigned char C> struct Overflow; // expected-note{{template parameter is declared here}}
93
94Overflow<5> *overflow1; // okay
95Overflow<255> *overflow2; // okay
96Overflow<256> *overflow3; // expected-warning{{non-type template argument value '256' truncated to '0' for template parameter of type 'unsigned char'}}
97
98
99template<unsigned> struct Signedness; // expected-note{{template parameter is declared here}}
100Signedness<10> *signedness1; // okay
101Signedness<-10> *signedness2; // expected-warning{{non-type template argument with value '-10' converted to '4294967286' for unsigned template parameter of type 'unsigned int'}}
102
103template<signed char C> struct SignedOverflow; // expected-note 3 {{template parameter is declared here}}
104SignedOverflow<1> *signedoverflow1;
105SignedOverflow<-1> *signedoverflow2;
106SignedOverflow<-128> *signedoverflow3;
107SignedOverflow<-129> *signedoverflow4; // expected-warning{{non-type template argument value '-129' truncated to '127' for template parameter of type 'signed char'}}
108SignedOverflow<127> *signedoverflow5;
109SignedOverflow<128> *signedoverflow6; // expected-warning{{non-type template argument value '128' truncated to '-128' for template parameter of type 'signed char'}}
110SignedOverflow<(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.
113template<int (*)(int, int)> struct FuncPtr0;
114int func0(int, int);
115extern FuncPtr0<&func0> *fp0;
116template<int (*)(int, int)> struct FuncPtr0;
117extern FuncPtr0<&func0> *fp0;
118int func0(int, int);
119extern FuncPtr0<&func0> *fp0;
120
121// PR5350
122namespace 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
137namespace 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
152template<int (*)(float)> struct X0 { };
153
154struct X1 {
155    static int pfunc(float);
156};
157void test_X0_X1() {
158  X0<X1::pfunc> x01;
159}
160
161// PR6249
162namespace 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
171namespace 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.
183namespace 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
198namespace 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
209namespace 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
248namespace 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
253namespace 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
267namespace 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
324template <int& I> struct PR10766 { static int *ip; };
325template <int& I> int* PR10766<I>::ip = &I;
326