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)'}}
82 template<int (Z::*pmf)(int)> struct A6; // expected-note{{template parameter is declared here}}
85 A6<&Z::baz> *a17_3; // expected-error-re{{non-type template argument of type 'double (Z::*)(double){{( __attribute__\(\(thiscall\)\))?}}' cannot be converted to a value of type 'int (Z::*)(int){{( __attribute__\(\(thiscall\)\))?}}'}}
88 template<int Z::*pm> struct A7; // expected-note{{template parameter is declared here}}
89 template<int Z::*pm> struct A7c;
90 A7<&Z::int_member> *a18_1;
91 A7c<&Z::int_member> *a18_2;
92 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::*'}}
93 A7c<(&Z::int_member)> *a18_4; // expected-warning{{address non-type template argument cannot be surrounded by parentheses}}
94 A7c<&Z::union_member> *a18_5;
96 template<unsigned char C> struct Overflow; // expected-note{{template parameter is declared here}}
98 Overflow<5> *overflow1; // okay
99 Overflow<255> *overflow2; // okay
100 Overflow<256> *overflow3; // expected-warning{{non-type template argument value '256' truncated to '0' for template parameter of type 'unsigned char'}}
103 template<unsigned> struct Signedness; // expected-note{{template parameter is declared here}}
104 Signedness<10> *signedness1; // okay
105 Signedness<-10> *signedness2; // expected-warning{{non-type template argument with value '-10' converted to '4294967286' for unsigned template parameter of type 'unsigned int'}}
107 template<signed char C> struct SignedOverflow; // expected-note 3 {{template parameter is declared here}}
108 SignedOverflow<1> *signedoverflow1;
109 SignedOverflow<-1> *signedoverflow2;
110 SignedOverflow<-128> *signedoverflow3;
111 SignedOverflow<-129> *signedoverflow4; // expected-warning{{non-type template argument value '-129' truncated to '127' for template parameter of type 'signed char'}}
112 SignedOverflow<127> *signedoverflow5;
113 SignedOverflow<128> *signedoverflow6; // expected-warning{{non-type template argument value '128' truncated to '-128' for template parameter of type 'signed char'}}
114 SignedOverflow<(unsigned char)128> *signedoverflow7; // expected-warning{{non-type template argument value '128' truncated to '-128' for template parameter of type 'signed char'}}
116 // Check canonicalization of template arguments.
117 template<int (*)(int, int)> struct FuncPtr0;
119 extern FuncPtr0<&func0> *fp0;
120 template<int (*)(int, int)> struct FuncPtr0;
121 extern FuncPtr0<&func0> *fp0;
123 extern FuncPtr0<&func0> *fp0;
127 template <typename T>
129 static const bool value = true;
135 const bool value = false;
137 Bar<bool(ns::Foo<int>::value)> x;
147 Baz<k> f1; // This works.
148 Baz<E(0)> f2; // This too.
149 Baz<static_cast<E>(0)> f3; // And this.
151 Baz<ns::E(0)> b1; // This doesn't work.
152 Baz<static_cast<ns::E>(0)> b2; // This neither.
156 template<int (*)(float)> struct X0 { };
159 static int pfunc(float);
167 template<typename T, T (*func)()> T f() {
172 template int f<int, h>();
176 template<unsigned char C> void f(int (&a)[C]); // expected-note 3{{candidate template ignored: substitution failure [with C = '\x00']}}
177 // expected-note@-1 {{not viable: no known conversion from 'int [512]' to 'int (&)[0]'}}
180 f(arr512); // expected-error{{no matching function for call}}
181 f<512>(arr512); // expected-error{{no matching function for call}}
184 f(arr0); // expected-error{{no matching function for call}}
185 f<0>(arr0); // expected-error{{no matching function for call}}
189 // Check that we instantiate declarations whose addresses are taken
190 // for non-type template arguments.
191 namespace EntityReferenced {
192 template<typename T, void (*)(T)> struct X { };
197 x = 1; // expected-error{{assigning to 'int *' from incompatible type 'int'}}
202 typedef X<int*, Y<int*>::f> x; // expected-note{{in instantiation of}}
207 template <typename ,int, int = 9223372036854775807L > // expected-warning 2{{non-type template argument value '9223372036854775807' truncated to '-1' for template parameter of type 'int'}} \
208 // expected-note 2{{template parameter is declared here}}
211 template <typename Sequence, int I0>
212 struct as_nview<Sequence, I0> // expected-note{{while checking a default template argument used here}}
216 // rdar://problem/8302138
218 template <int* ip> struct A {
240 template <C* cp> struct B {
257 template <int I> void foo() { } // expected-note{{template parameter is declared here}}
258 void bar() { foo <0x80000000> (); } // expected-warning{{non-type template argument value '2147483648' truncated to '-2147483648' for template parameter of type 'int'}}
262 template <bool B> struct enable_if_bool { };
263 template <> struct enable_if_bool<true> { typedef int type; }; // expected-note{{'enable_if_bool<true>::type' declared here}}
264 void test_bool() { enable_if_bool<false>::type i; } // expected-error{{enable_if_bool<false>'; did you mean 'enable_if_bool<true>::type'?}}
266 template <char C> struct enable_if_char { };
267 template <> struct enable_if_char<'a'> { typedef int type; }; // expected-note 5{{'enable_if_char<'a'>::type' declared here}}
268 void test_char_0() { enable_if_char<0>::type i; } // expected-error{{enable_if_char<'\x00'>'; did you mean 'enable_if_char<'a'>::type'?}}
269 void test_char_b() { enable_if_char<'b'>::type i; } // expected-error{{enable_if_char<'b'>'; did you mean 'enable_if_char<'a'>::type'?}}
270 void test_char_possibly_negative() { enable_if_char<'\x02'>::type i; } // expected-error{{enable_if_char<'\x02'>'; did you mean 'enable_if_char<'a'>::type'?}}
271 void test_char_single_quote() { enable_if_char<'\''>::type i; } // expected-error{{enable_if_char<'\''>'; did you mean 'enable_if_char<'a'>::type'?}}
272 void test_char_backslash() { enable_if_char<'\\'>::type i; } // expected-error{{enable_if_char<'\\'>'; did you mean 'enable_if_char<'a'>::type'?}}
322 typedef fcppt::container::bitfield::basic<
332 template <int& I> struct PR10766 { static int *ip; };
333 template <int& I> int* PR10766<I>::ip = &I;
335 namespace rdar13000548 {
336 template<typename R, typename U, R F>
337 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 *'}}
343 f<int(int), int (*)(int), g>(); // expected-note{{in instantiation of}}
344 f<int[3], int*, y>(); // expected-note{{in instantiation of}}
349 namespace rdar13806270 {
350 template <unsigned N> class X { };
351 const unsigned value = 32;
365 template <int (a::*p)> struct b : a {
366 b() { this->*p = 0; }
372 namespace partial_order_different_types {
373 template<int, int, typename T, typename, T> struct A;
374 template<int N, typename T, typename U, T V> struct A<0, N, T, U, V>; // expected-note {{matches}}
375 // FIXME: It appears that this partial specialization should be ill-formed as
376 // it is not more specialized than the primary template. V is not deducible
377 // because it does not have the same type as the corresponding parameter.
378 template<int N, typename T, typename U, U V> struct A<0, N, T, U, V> {}; // expected-note {{matches}}
379 A<0, 0, int, int, 0> a; // expected-error {{ambiguous}}
382 namespace partial_order_references {
383 // FIXME: The standard does not appear to consider the second specialization
384 // to be more more specialized than the first! The problem is that deducing
385 // an 'int&' parameter from an argument 'R' results in a type mismatch,
386 // because the parameter has a reference type and the argument is an
387 // expression and thus does not have reference type. We resolve this by
388 // matching the type of an expression corresponding to the parameter rather
389 // than matching the parameter itself.
390 template <int, int, int &> struct A {};
391 template <int N, int &R> struct A<N, 0, R> {};
392 template <int &R> struct A<0, 0, R> {};
396 template<int, int &R> struct B; // expected-note 2{{template}}
397 template<const int &R> struct B<0, R> {};
398 // expected-error@-1 {{not more specialized than the primary}}
399 // expected-note@-2 {{'const int' vs 'int &'}}
400 B<0, N> b; // expected-error {{undefined}}
402 template<int, const int &R> struct C; // expected-note 2{{template}}
403 template<int &R> struct C<0, R> {};
404 // expected-error@-1 {{not more specialized than the primary}}
405 // expected-note@-2 {{'int' vs 'const int &'}}
406 C<0, N> c; // expected-error {{undefined}}
408 template<int, const int &R> struct D; // expected-note 2{{template}}
409 template<int N> struct D<0, N> {};
410 // expected-error@-1 {{not more specialized than the primary}}
411 // expected-note@-2 {{'int' vs 'const int &'}}
412 extern const int K = 5;
413 D<0, K> d; // expected-error {{undefined}}
416 namespace dependent_nested_partial_specialization {
417 template<typename> using X = int; // expected-warning {{C++11}}
418 template<typename T> using Y = T*; // expected-warning {{C++11}}
421 template<template<typename> class X> struct A {
422 template<typename T, X<T> N> struct B; // expected-note 2{{here}}
423 template<typename T> struct B<T, 0> {}; // expected-error {{specializes a template parameter with dependent type 'Y<T>'}}
426 A<Y>::B<int, &n> ay; // expected-error {{undefined}} expected-note {{instantiation of}}
428 template<template<typename> class X> struct C {
429 template<typename T, int N, int M> struct D; // expected-note {{here}}
430 template<typename T, X<T> N> struct D<T*, N, N + 1> {}; // expected-error {{type of specialized non-type template argument depends on}}
432 C<X>::D<int*, 0, 1> cx;
433 C<Y>::D<int*, 0, 1> cy; // expected-error {{undefined}} expected-note {{instantiation of}}
435 template<typename T> struct E {
436 template<typename U, U V> struct F; // expected-note {{template}}
437 template<typename W, T V> struct F<W, V> {}; // expected-error {{not more specialized than the primary}}
439 E<int>::F<int, 0> e1; // expected-note {{instantiation of}}
442 namespace nondependent_default_arg_ordering {
444 template<typename A, A B = &n> struct X {};
445 template<typename A> void f(X<A>); // expected-note {{candidate}}
446 template<typename A> void f(X<A, &m>); // expected-note {{candidate}}
447 template<typename A, A B> void f(X<A, B>); // expected-note 2{{candidate}}
448 template<template<typename U, U> class T, typename A, int *B> void f(T<A, B>); // expected-note 2{{candidate}}
450 // FIXME: The first and second function templates above should be
451 // considered more specialized than the last two, but during partial
452 // ordering we fail to check that we actually deduced template arguments
453 // that make the deduced A identical to A.
454 X<int *, &n> x; f(x); // expected-error {{ambiguous}}
455 X<int *, &m> y; f(y); // expected-error {{ambiguous}}
459 namespace pointer_to_char_array {
461 template<T *P> struct A { void f(); };
462 template<T *P> void A<P>::f() {}
464 void g() { A<&foo>().f(); }