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}}
6 A<int()> *a1; // expected-error{{template argument for non-type template parameter is treated as function type 'int ()'}}
8 A<int> *a2; // expected-error{{template argument for non-type template parameter must be an expression}}
10 A<1 >> 2> *a3; // expected-warning{{use of right-shift operator ('>>') in template argument will require parentheses in C++11}}
12 // C++ [temp.arg.nontype]p5:
13 A<A> *a4; // expected-error{{must be an expression}}
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'}}
21 const long LongValue = 12345678;
23 const short ShortValue = 17;
27 A<f(17)> *a10; // expected-error{{non-type template argument of type 'int' is not an integral constant expression}}
35 A<X(17, 42)> *a11; // expected-error{{non-type template argument of type 'X' must have an integral or enumeration type}}
39 float g(float); // expected-note 2{{candidate function}}
40 double g(double); // expected-note 2{{candidate function}}
45 template<int fp(int)> struct A3; // expected-note 1{{template parameter is declared here}}
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)'}}
56 volatile X * X_volatile_ptr;
57 template<X const &AnX> struct A4; // expected-note 2{{template parameter is declared here}}
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}}
64 template<int (&fr)(int)> struct A5; // expected-note{{template parameter is declared here}}
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)'}}
79 template<int (Z::*pmf)(int)> struct A6; // expected-note{{template parameter is declared here}}
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)'}}
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}}
92 template<unsigned char C> struct Overflow; // expected-note{{template parameter is declared here}}
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'}}
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'}}
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'}}
112 // Check canonicalization of template arguments.
113 template<int (*)(int, int)> struct FuncPtr0;
115 extern FuncPtr0<&func0> *fp0;
116 template<int (*)(int, int)> struct FuncPtr0;
117 extern FuncPtr0<&func0> *fp0;
119 extern FuncPtr0<&func0> *fp0;
123 template <typename T>
125 static const bool value = true;
131 const bool value = false;
133 Bar<bool(ns::Foo<int>::value)> x;
143 Baz<k> f1; // This works.
144 Baz<E(0)> f2; // This too.
145 Baz<static_cast<E>(0)> f3; // And this.
147 Baz<ns::E(0)> b1; // This doesn't work.
148 Baz<static_cast<ns::E>(0)> b2; // This neither.
152 template<int (*)(float)> struct X0 { };
155 static int pfunc(float);
163 template<typename T, T (*func)()> T f() {
168 template int f<int, h>();
172 template<unsigned char C> void f(int (&a)[C]); // expected-note {{candidate template ignored}} \
173 // expected-note{{substitution failure [with C = '\x00']}}
176 f(arr512); // expected-error{{no matching function for call}}
177 f<512>(arr512); // expected-error{{no matching function for call}}
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 { };
189 x = 1; // expected-error{{assigning to 'int *' from incompatible type 'int'}}
194 typedef X<int*, Y<int*>::f> x; // expected-note{{in instantiation of}}
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}}
203 template <typename Sequence, int I0>
204 struct as_nview<Sequence, I0> // expected-note{{while checking a default template argument used here}}
208 // rdar://problem/8302138
210 template <int* ip> struct A {
232 template <C* cp> struct B {
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'}}
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>}}
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<'\\'>}}
314 typedef fcppt::container::bitfield::basic<
324 template <int& I> struct PR10766 { static int *ip; };
325 template <int& I> int* PR10766<I>::ip = &I;
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 *'}}
335 f<int(int), int (*)(int), g>(); // expected-note{{in instantiation of}}
336 f<int[3], int*, y>(); // expected-note{{in instantiation of}}
341 namespace rdar13806270 {
342 template <unsigned N> class X { };
343 const unsigned value = 32;