[RISCV][FMV] Support target_clones (#85786)
[llvm-project.git] / clang / test / SemaTemplate / default-arguments.cpp
blob3b1fbda414c12bf710ce954e442ba570ddbfdbb3
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4 template<typename T, int N = 2> struct X; // expected-note{{template is declared here}}
6 X<int, 1> *x1;
7 X<int> *x2;
9 X<> *x3; // expected-error{{too few template arguments for class template 'X'}}
11 template<typename U = float, int M> struct X;
13 X<> *x4;
15 template<typename T = int> struct Z { };
16 template struct Z<>;
18 // PR4362
19 template<class T> struct a { };
20 template<> struct a<int> { static const bool v = true; };
22 template<class T, bool = a<T>::v> struct p { }; // expected-error {{no member named 'v'}}
24 template struct p<bool>; // expected-note {{in instantiation of default argument for 'p<bool>' required here}}
25 template struct p<int>;
27 // PR5187
28 template<typename T, typename U>
29 struct A;
31 template<typename T, typename U = T>
32 struct A;
34 template<typename T, typename U>
35 struct A {
36 void f(A<T>);
39 template<typename T>
40 struct B { };
42 template<>
43 struct B<void> {
44 typedef B<void*> type;
47 // Nested default arguments for template parameters.
48 template<typename T> struct X1 { };
50 template<typename T>
51 struct X2 {
52 template<typename U = typename X1<T>::type> // expected-error{{no type named 'type' in 'X1<int>'}} \
53 // expected-error{{no type named 'type' in 'X1<char>'}}
54 struct Inner1 { }; // expected-note{{template is declared here}}
56 template<T Value = X1<T>::value> // expected-error{{no member named 'value' in 'X1<int>'}} \
57 // expected-error{{no member named 'value' in 'X1<char>'}}
58 struct NonType1 { }; // expected-note{{template is declared here}}
60 template<T Value>
61 struct Inner2 { };
63 template<typename U>
64 struct Inner3 {
65 template<typename X = T, typename V = U>
66 struct VeryInner { };
68 template<T Value1 = sizeof(T), T Value2 = sizeof(U),
69 T Value3 = Value1 + Value2>
70 struct NonType2 { };
74 X2<int> x2i; // expected-note{{in instantiation of template class 'X2<int>' requested here}}
75 X2<int>::Inner1<float> x2iif;
77 X2<int>::Inner1<> x2bad; // expected-error{{too few template arguments for class template 'Inner1'}}
79 X2<int>::NonType1<'a'> x2_nontype1;
80 X2<int>::NonType1<> x2_nontype1_bad; // expected-error{{too few template arguments for class template 'NonType1'}}
82 // Check multi-level substitution into template type arguments
83 X2<int>::Inner3<float>::VeryInner<> vi;
84 X2<char>::Inner3<int>::NonType2<> x2_deep_nontype; // expected-note{{in instantiation of template class 'X2<char>' requested here}}
86 template<typename T, typename U>
87 struct is_same { static const bool value = false; };
89 template<typename T>
90 struct is_same<T, T> { static const bool value = true; };
92 int array1[is_same<__typeof__(vi),
93 X2<int>::Inner3<float>::VeryInner<int, float> >::value? 1 : -1];
95 int array2[is_same<__typeof(x2_deep_nontype),
96 X2<char>::Inner3<int>::NonType2<sizeof(char), sizeof(int),
97 sizeof(char)+sizeof(int)> >::value? 1 : -1];
99 // Template template parameter defaults
100 template<template<typename T> class X = X2> struct X3 { };
101 int array3[is_same<X3<>, X3<X2> >::value? 1 : -1];
103 struct add_pointer {
104 template<typename T>
105 struct apply {
106 typedef T* type;
110 template<typename T, template<typename> class X = T::template apply>
111 struct X4;
112 int array4[is_same<X4<add_pointer>,
113 X4<add_pointer, add_pointer::apply> >::value? 1 : -1];
115 template<int> struct X5 {};
116 template<long> struct X5b {};
117 template<typename T,
118 template<T> class B = X5>
119 struct X6 {};
121 X6<int> x6a;
122 X6<long> x6b;
123 X6<long, X5b> x6c;
126 template<template<class> class X = B<int> > struct X7; // expected-error{{must be a class template}}
128 namespace PR9643 {
129 template<typename T> class allocator {};
130 template<typename T, typename U = allocator<T> > class vector {};
132 template<template<typename U, typename = allocator<U> > class container,
133 typename DT>
134 container<DT> initializer(const DT& d) {
135 return container<DT>();
138 void f() {
139 vector<int, allocator<int> > v = initializer<vector>(5);
143 namespace PR16288 {
144 template<typename X>
145 struct S {
146 template<typename T = int, typename U>
147 #if __cplusplus <= 199711L // C++03 or earlier modes
148 // expected-warning@-2 {{default template arguments for a function template are a C++11 extension}}
149 #endif
150 void f();
152 template<typename X>
153 template<typename T, typename U>
154 void S<X>::f() {}
157 namespace DR1635 {
158 template <class T> struct X {
159 template <class U = typename T::type> static void f(int) {} // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
160 #if __cplusplus <= 199711L // C++03 or earlier modes
161 // expected-warning@-2 {{default template arguments for a function template are a C++11 extension}}
162 #endif
163 static void f(...) {}
166 int g() { X<int>::f(0); } // expected-note {{in instantiation of template class 'DR1635::X<int>' requested here}}
169 namespace NondefDecls {
170 template<typename T> void f1() {
171 int g1(int defarg = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}} \
172 // expected-note {{in instantiation of default function argument expression for 'g1<int>' required here}}
174 template void f1<int>(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1<int>' requested here}}
177 template <typename T>
178 struct C {
179 C(T t = ); // expected-error {{expected expression}}
181 C<int> obj;
183 namespace PR26134 {
184 // Make sure when substituting default template arguments we do it in the current context.
185 template<class T, bool Val = T::value>
186 struct X {};
188 template<bool B> struct Y {
189 void f() { X<Y> xy; }
190 static const bool value = B;
193 namespace ns1 {
194 template<class T0>
195 struct X {
196 template<bool B = T0::value> struct XInner { static const bool value = B; };
198 template<bool B> struct S { static const bool value = B; };
199 #if __cplusplus > 199711L
200 template<bool B> struct Y {
201 static constexpr bool f() { return typename X<S<B>>::template XInner<>{}.value; }
202 static_assert(f() == B, "");
204 Y<true> y;
205 Y<false> y2;
206 #endif
208 } // end ns1
209 } // end ns PR26134
211 namespace friends {
212 namespace ns {
213 template<typename> struct A {
214 template<typename> friend void f();
215 template<typename> friend struct X;
217 template<typename = int> void f(); // expected-warning 0-1{{extension}}
218 template<typename = int> struct X;
219 A<int> a;
221 namespace ns {
222 void g() { f(); }
223 X<int> *p;
227 namespace unevaluated {
228 int a;
229 template<int = 0> int f(int = a); // expected-warning 0-1{{extension}}
230 int k = sizeof(f());
233 #if __cplusplus >= 201103L
234 namespace GH68490 {
236 template <typename T> struct S {
237 template <typename U>
238 constexpr int SizeOfU(int param = sizeof(U)) const;
240 template <typename U>
241 constexpr int SizeOfT(int param = sizeof(T)) const;
244 template <typename T> struct S<T *> {
245 template <typename U>
246 constexpr int SizeOfU(int param = sizeof(U)) const;
248 template <typename U>
249 constexpr int SizeOfT(int param = sizeof(T *)) const;
252 template <typename T>
253 template <typename U>
254 constexpr int S<T *>::SizeOfU(int param) const {
255 return param;
258 template <typename T>
259 template <typename U>
260 constexpr int S<T *>::SizeOfT(int param) const {
261 return param;
264 template <>
265 template <typename T>
266 constexpr int S<int>::SizeOfU(int param) const {
267 return param;
270 template <>
271 template <typename T>
272 constexpr int S<int>::SizeOfT(int param) const {
273 return param;
276 static_assert(S<int>().SizeOfU<char>() == sizeof(char), "");
277 static_assert(S<int>().SizeOfT<char>() == sizeof(int), "");
278 static_assert(S<short *>().SizeOfU<char>() == sizeof(char), "");
279 static_assert(S<short *>().SizeOfT<char>() == sizeof(short *), "");
281 } // namespace GH68490
283 #endif