1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
4 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z %s
5 // RUN: %clang_cc1 -fsyntax-only -verify %s
7 // C++0x [temp.arg.nontype] p5:
8 // The following conversions are performed on each expression used as
9 // a non-type template-argument. If a non-type template-argument cannot be
10 // converted to the type of the corresponding template-parameter then the
11 // program is ill-formed.
12 // -- for a non-type template-parameter of integral or enumeration type,
13 // integral promotions (4.5) and integral conversions (4.7) are applied.
14 namespace integral_parameters {
15 template<short s> struct X0 { };
18 template<char c> struct X1 { };
22 // -- for a non-type template-parameter of type pointer to object,
23 // qualification conversions (4.4) and the array-to-pointer conversion
24 // (4.2) are applied; if the template-argument is of type
25 // std::nullptr_t, the null pointer conversion (4.10) is applied.
26 namespace pointer_to_object_parameters {
32 template<const char *s>
34 Str get() { return s; }
37 char hello[6] = "Hello";
38 extern const char world[6];
39 const char world[6] = "world";
41 (void)A<hello>().get();
42 (void)A<world>().get();
52 template<X const *Ptr> struct A2; // expected-note 0-1{{template parameter is declared here}}
54 X *X_ptr; // expected-note 0-1{{declared here}}
58 #if __cplusplus < 201103L
59 // expected-error@-2 {{must have its address taken}}
61 // expected-error@-4 {{not a constant expression}} expected-note@-4 {{read of non-constexpr variable}}
66 #if __cplusplus < 201103L
67 // expected-warning@-2 {{address non-type template argument cannot be surrounded by parentheses}}
72 template <X1*> struct X2 { };
73 template <X1* Value> struct X3 : X2<Value> { };
74 struct X4 : X3<&X1v> { };
77 int *bar; // expected-note 0-1{{declared here}}
78 template <int *> struct zed {}; // expected-note 0-2{{template parameter is declared here}}
80 #if __cplusplus < 201103L
81 // expected-error@-2 {{must have its address taken}}
83 // expected-error@-4 {{not a constant expression}} expected-note@-4 {{read of non-constexpr variable}}
86 int baz; // expected-note 0-1{{declared here}}
88 #if __cplusplus < 201103L
89 // expected-error@-2 {{must have its address taken}}
90 #elif __cplusplus <= 201402L
91 // expected-error@-4 {{not a constant expression}} expected-note@-4 {{read of non-const variable}}
93 // expected-error@-6 {{not implicitly convertible to 'int *'}}
96 void g3(zed<&baz>*); // okay
99 // -- For a non-type template-parameter of type reference to object, no
100 // conversions apply. The type referred to by the reference may be more
101 // cv-qualified than the (otherwise identical) type of the
102 // template-argument. The template-parameter is bound directly to the
103 // template-argument, which shall be an lvalue.
104 namespace reference_parameters {
105 template <int& N> struct S0 { }; // expected-note 0-3{{template parameter is declared here}}
106 template <const int& N> struct S1 { }; // expected-note 0-2{{template parameter is declared here}}
107 template <volatile int& N> struct S2 { }; // expected-note 0-2{{template parameter is declared here}}
108 template <const volatile int& N> struct S3 { };
112 extern const volatile int cvi;
115 S0<ci> s0c; // expected-error{{type 'const int'}}
116 S0<vi> s0v; // expected-error{{type 'volatile int'}}
117 S0<cvi> s0cv; // expected-error{{type 'const volatile int'}}
121 S1<vi> s1v; // expected-error{{type 'volatile int'}}
122 S1<cvi> s1cv; // expected-error{{type 'const volatile int'}}
125 S2<ci> s2c; // expected-error{{type 'const int'}}
127 S2<cvi> s2cv; // expected-error{{type 'const volatile int'}}
136 template <typename T, const T &ref> void inc() {
137 ref++; // expected-error{{read-only variable is not assignable}}
140 template<typename T, const T &ref> void bind() {
141 T &ref2 = ref; // expected-error{{drops 'const' qualifier}}
146 inc<int, counter>(); // expected-note{{instantiation of}}
147 bind<int, counter>(); // expected-note{{instantiation of}}
152 template <int& i> struct foo {}; // expected-note 0-1{{template parameter is declared here}}
155 #if __cplusplus <= 201402L
156 // expected-error@-2 {{is not an object}}
161 // -- For a non-type template-parameter of type pointer to function, the
162 // function-to-pointer conversion (4.3) is applied; if the
163 // template-argument is of type std::nullptr_t, the null pointer
164 // conversion (4.10) is applied. If the template-argument represents
165 // a set of overloaded functions (or a pointer to such), the matching
166 // function is selected from the set (13.4).
167 namespace pointer_to_function {
168 template<int (*)(int)> struct X0 { }; // expected-note 0-3{{template parameter is declared here}}
172 int (*funcptr)(int); // expected-note 0-1{{declared here}}
175 void x0c(X0<g>); // expected-error-re{{type 'int (float)' {{.*}}convert{{.*}} 'int (*)(int)'}}
176 void x0d(X0<&g>); // expected-error-re{{type 'int (*)(float)' {{.*}}convert{{.*}} 'int (*)(int)'}}
177 void x0e(X0<funcptr>);
178 #if __cplusplus < 201103L
179 // expected-error@-2 {{must have its address taken}}
181 // expected-error@-4 {{not a constant expression}} expected-note@-4 {{read of non-constexpr variable}}
185 // -- For a non-type template-parameter of type reference to function, no
186 // conversions apply. If the template-argument represents a set of
187 // overloaded functions, the matching function is selected from the set
189 namespace reference_to_function {
190 template<int (&)(int)> struct X0 { }; // expected-note 0-4{{template parameter is declared here}}
196 #if __cplusplus <= 201402L
197 void x0b(X0<&f>); // expected-error{{address taken in non-type template argument for template parameter of reference type 'int (&)(int)'}}
198 void x0c(X0<g>); // expected-error{{non-type template parameter of reference type 'int (&)(int)' cannot bind to template argument of type 'int (float)'}}
199 void x0d(X0<&g>); // expected-error{{address taken in non-type template argument for template parameter of reference type 'int (&)(int)'}}
200 void x0e(X0<funcptr>); // expected-error{{non-type template parameter of reference type 'int (&)(int)' cannot bind to template argument of type 'int (*)(int)'}}
202 void x0b(X0<&f>); // expected-error{{value of type '<overloaded function type>' is not implicitly convertible to 'int (&)(int)'}}
203 void x0c(X0<g>); // expected-error{{value of type 'int (float)' is not implicitly convertible to 'int (&)(int)'}}
204 void x0d(X0<&g>); // expected-error{{value of type 'int (*)(float)' is not implicitly convertible to 'int (&)(int)'}}
205 void x0e(X0<funcptr>); // expected-error{{value of type 'int (*)(int)' is not implicitly convertible to 'int (&)(int)'}}
208 // -- For a non-type template-parameter of type pointer to member function,
209 // if the template-argument is of type std::nullptr_t, the null member
210 // pointer conversion (4.11) is applied; otherwise, no conversions
211 // apply. If the template-argument represents a set of overloaded member
212 // functions, the matching member function is selected from the set
214 namespace pointer_to_member_function {
223 template<int (Y::*)(int)> struct X0 {}; // expected-note 0-1{{template parameter is declared here}}
226 X0<&Y::h> x0c; // expected-error-re{{type 'float (pointer_to_member_function::Y::*)(float){{( __attribute__\(\(thiscall\)\))?}}' {{.*}} convert{{.*}} 'int (pointer_to_member_function::Y::*)(int){{( __attribute__\(\(thiscall\)\))?}}'}}
229 // -- For a non-type template-parameter of type pointer to data member,
230 // qualification conversions (4.4) are applied; if the template-argument
231 // is of type std::nullptr_t, the null member pointer conversion (4.11)
233 namespace pointer_to_member_data {
235 struct Y : X { int y; };
237 template<int Y::*> struct X0 {}; // expected-note 0-1{{template parameter is declared here}}
240 #if __cplusplus <= 201402L
241 // expected-error@-2 {{non-type template argument of type 'int pointer_to_member_data::X::*' cannot be converted to a value of type 'int pointer_to_member_data::Y::*'}}
243 // expected-error@-4 {{conversion from 'int pointer_to_member_data::X::*' to 'int pointer_to_member_data::Y::*' is not allowed in a converted constant expression}}
246 // Test qualification conversions
247 template<const int Y::*> struct X1 {};