Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaTemplate / alias-templates.cpp
blob8d7cc6118610a07755a8d7e8cb60e0931a565d71
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
3 template<typename S>
4 struct A {
5 typedef S B;
6 template<typename T> using C = typename T::B;
7 template<typename T> struct D {
8 template<typename U> using E = typename A<U>::template C<A<T>>;
9 template<typename U> using F = A<E<U>>;
10 template<typename U> using G = C<F<U>>;
11 G<T> g;
13 typedef decltype(D<B>().g) H;
14 D<H> h;
15 template<typename T> using I = A<decltype(h.g)>;
16 template<typename T> using J = typename A<decltype(h.g)>::template C<I<T>>;
19 A<int> a;
20 A<char>::D<double> b;
22 template<typename T> T make();
24 namespace X {
25 template<typename T> struct traits {
26 typedef T thing;
27 typedef decltype(val(make<thing>())) inner_ptr;
29 template<typename U> using rebind_thing = typename thing::template rebind<U>;
30 template<typename U> using rebind = traits<rebind_thing<U>>;
32 inner_ptr &&alloc();
33 void free(inner_ptr&&);
36 template<typename T> struct ptr_traits {
37 typedef T *type;
39 template<typename T> using ptr = typename ptr_traits<T>::type;
41 template<typename T> struct thing {
42 typedef T inner;
43 typedef ptr<inner> inner_ptr;
44 typedef traits<thing<inner>> traits_type;
46 template<typename U> using rebind = thing<U>;
48 thing(traits_type &traits) : traits(traits), val(traits.alloc()) {}
49 ~thing() { traits.free(static_cast<inner_ptr&&>(val)); }
51 traits_type &traits;
52 inner_ptr val;
54 friend inner_ptr val(const thing &t) { return t.val; }
57 template<> struct ptr_traits<bool> {
58 typedef bool &type;
60 template<> bool &traits<thing<bool>>::alloc() { static bool b; return b; }
61 template<> void traits<thing<bool>>::free(bool&) {}
64 typedef X::traits<X::thing<int>> itt;
66 itt::thing::traits_type itr;
67 itt::thing ith(itr);
69 itt::rebind<bool> btr;
70 itt::rebind_thing<bool> btt(btr);
72 namespace PR11848 {
73 template<typename T> using U = int;
75 template<typename T, typename ...Ts>
76 void f1(U<T> i, U<Ts> ...is) { // expected-note 2{{couldn't infer template argument 'T'}}
77 return i + f1<Ts...>(is...);
80 template<typename ...Ts>
81 void f2(U<Ts> ...is) { } // expected-note {{deduced incomplete pack <(no value)> for template parameter 'Ts'}}
83 template<typename...> struct type_tuple {};
84 template<typename ...Ts>
85 void f3(type_tuple<Ts...>, U<Ts> ...is) {} // expected-note {{deduced packs of different lengths for parameter 'Ts' (<void, void, void> vs. <(no value), (no value)>)}}
87 void g() {
88 f1(U<void>()); // expected-error {{no match}}
89 f1(1, 2, 3, 4, 5); // expected-error {{no match}}
90 f2(); // ok
91 f2(1); // expected-error {{no match}}
92 f3(type_tuple<>());
93 f3(type_tuple<void, void, void>(), 1, 2); // expected-error {{no match}}
94 f3(type_tuple<void, void, void>(), 1, 2, 3);
97 template<typename ...Ts>
98 struct S {
99 S(U<Ts>...ts);
102 template<typename T>
103 struct Hidden1 {
104 template<typename ...Ts>
105 Hidden1(typename T::template U<Ts> ...ts);
108 template<typename T, typename ...Ts>
109 struct Hidden2 {
110 Hidden2(typename T::template U<Ts> ...ts);
113 struct Hide {
114 template<typename T> using U = int;
117 Hidden1<Hide> h1;
118 Hidden2<Hide, double, char> h2(1, 2);
121 namespace Core22036 {
122 struct X {};
123 void h(...);
124 template<typename T> using Y = X;
125 template<typename T, typename ...Ts> struct S {
126 // An expression can contain an unexpanded pack without being type or
127 // value dependent. This is true even if the expression's type is a pack
128 // expansion type.
129 void f1(Y<T> a) { h(g(a)); } // expected-error {{undeclared identifier 'g'}}
130 void f2(Y<Ts>...as) { h(g(as)...); } // expected-error {{undeclared identifier 'g'}}
131 void f3(Y<Ts>...as) { g(as...); } // ok
132 void f4(Ts ...ts) { h(g(sizeof(ts))...); } // expected-error {{undeclared identifier 'g'}}
133 // FIXME: We can reject this, since it has no valid instantiations because
134 // 'g' never has any associated namespaces.
135 void f5(Ts ...ts) { g(sizeof(ts)...); } // ok
139 namespace PR13243 {
140 template<typename A> struct X {};
141 template<int I> struct C {};
142 template<int I> using Ci = C<I>;
144 template<typename A, int I> void f(X<A>, Ci<I>) {}
145 template void f(X<int>, C<0>);
148 namespace PR13136 {
149 template <typename T, T... Numbers>
150 struct NumberTuple { };
152 template <unsigned int... Numbers>
153 using MyNumberTuple = NumberTuple<unsigned int, Numbers...>;
155 template <typename U, unsigned int... Numbers>
156 void foo(U&&, MyNumberTuple<Numbers...>);
158 template <typename U, unsigned int... Numbers>
159 void bar(U&&, NumberTuple<unsigned int, Numbers...>);
161 int main() {
162 foo(1, NumberTuple<unsigned int, 0, 1>());
163 bar(1, NumberTuple<unsigned int, 0, 1>());
164 return 0;
168 namespace PR16646 {
169 namespace test1 {
170 template <typename T> struct DefaultValue { const T value=0;};
171 template <typename ... Args> struct tuple {};
172 template <typename ... Args> using Zero = tuple<DefaultValue<Args> ...>;
173 template <typename ... Args> void f(const Zero<Args ...> &t);
174 void f() {
175 f(Zero<int,double,double>());
179 namespace test2 {
180 template<int x> struct X {};
181 template <template<int x> class temp> struct DefaultValue { const temp<0> value; };
182 template <typename ... Args> struct tuple {};
183 template <template<int x> class... Args> using Zero = tuple<DefaultValue<Args> ...>;
184 template <template<int x> class... Args> void f(const Zero<Args ...> &t);
185 void f() {
186 f(Zero<X,X,X>());
191 namespace PR16904 {
192 template <typename,typename>
193 struct base {
194 template <typename> struct derived;
196 template <typename T, typename U, typename V>
197 using derived = base<T, U>::template derived<V>; // expected-warning {{missing 'typename'}}
198 template <typename T, typename U, typename V>
199 using derived2 = ::PR16904::base<T, U>::template derived<V>; // expected-warning {{missing 'typename'}}
202 namespace PR14858 {
203 template<typename ...T> using X = int[sizeof...(T)];
205 template<typename ...U> struct Y {
206 using Z = X<U...>;
208 using A = Y<int, int, int, int>::Z;
209 using A = int[4];
211 // FIXME: These should be treated as being redeclarations.
212 template<typename ...T> void f(X<T...> &) {}
213 template<typename ...T> void f(int(&)[sizeof...(T)]) {}
215 template<typename ...T> void g(X<typename T::type...> &) {}
216 template<typename ...T> void g(int(&)[sizeof...(T)]) {} // ok, different
218 template<typename ...T, typename ...U> void h(X<T...> &) {}
219 template<typename ...T, typename ...U> void h(X<U...> &) {} // ok, different
221 template<typename ...T> void i(auto (T ...t) -> int(&)[sizeof...(t)]);
222 auto mk_arr(int, int) -> int(&)[2];
223 void test_i() { i<int, int>(mk_arr); }
225 #if 0 // FIXME: This causes clang to assert.
226 template<typename ...T> using Z = auto (T ...p) -> int (&)[sizeof...(p)];
227 template<typename ...T, typename ...U> void j(Z<T..., U...> &) {}
228 void test_j() { j<int, int>(mk_arr); }
229 #endif
231 template<typename ...T> struct Q {
232 template<typename ...U> using V = int[sizeof...(U)];
233 template<typename ...U> void f(V<typename U::type..., typename T::type...> *);
235 struct B { typedef int type; };
236 void test_q(int (&a)[5]) { Q<B, B, B>().f<B, B>(&a); }
239 namespace redecl {
240 template<typename> using A = int;
241 template<typename = void> using A = int;
242 A<> a; // ok
245 namespace PR31514 {
246 template<typename T, typename> using EnableTupleSize = T;
248 template<typename T> struct tuple_size { static const int value = 0; };
249 template<typename T> struct tuple_size<EnableTupleSize<const T, decltype(tuple_size<T>::value)>> {};
250 template<typename T> struct tuple_size<EnableTupleSize<volatile T, decltype(tuple_size<T>::value)>> {};
252 tuple_size<const int> t;
255 namespace an_alias_template_is_not_a_class_template {
256 template<typename T> using Foo = int; // expected-note 3{{here}}
257 Foo x; // expected-error {{use of alias template 'Foo' requires template arguments}}
258 Foo<> y; // expected-error {{too few template arguments for alias template 'Foo'}}
259 int z = Foo(); // expected-error {{use of alias template 'Foo' requires template arguments}}
261 template<template<typename> class Bar> void f() { // expected-note 3{{here}}
262 Bar x; // expected-error {{use of template template parameter 'Bar' requires template arguments}}
263 Bar<> y; // expected-error {{too few template arguments for template template parameter 'Bar'}}
264 int z = Bar(); // expected-error {{use of template template parameter 'Bar' requires template arguments}}
268 namespace resolved_nttp {
269 template <typename T> struct A {
270 template <int N> using Arr = T[N];
271 Arr<3> a;
273 using TA = decltype(A<int>::a);
274 using TA = int[3];
276 template <typename T> struct B {
277 template <int... N> using Fn = T(int(*...A)[N]);
278 Fn<1, 2, 3> *p;
280 using TB = decltype(B<int>::p);
281 using TB = int (*)(int (*)[1], int (*)[2], int (*)[3]);
283 template <typename T, int ...M> struct C {
284 template <T... N> using Fn = T(int(*...A)[N]);
285 Fn<1, M..., 4> *p; // expected-error-re 3{{evaluates to {{[234]}}, which cannot be narrowed to type 'bool'}}
287 using TC = decltype(C<int, 2, 3>::p);
288 using TC = int (*)(int (*)[1], int (*)[2], int (*)[3], int (*)[4]);
290 using TC2 = decltype(C<bool, 2, 3>::p); // expected-note {{instantiation of}}