xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/alias-templates.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc template<typename S>
4*f4a2713aSLionel Sambuc struct A {
5*f4a2713aSLionel Sambuc   typedef S B;
6*f4a2713aSLionel Sambuc   template<typename T> using C = typename T::B;
7*f4a2713aSLionel Sambuc   template<typename T> struct D {
8*f4a2713aSLionel Sambuc     template<typename U> using E = typename A<U>::template C<A<T>>;
9*f4a2713aSLionel Sambuc     template<typename U> using F = A<E<U>>;
10*f4a2713aSLionel Sambuc     template<typename U> using G = C<F<U>>;
11*f4a2713aSLionel Sambuc     G<T> g;
12*f4a2713aSLionel Sambuc   };
13*f4a2713aSLionel Sambuc   typedef decltype(D<B>().g) H;
14*f4a2713aSLionel Sambuc   D<H> h;
15*f4a2713aSLionel Sambuc   template<typename T> using I = A<decltype(h.g)>;
16*f4a2713aSLionel Sambuc   template<typename T> using J = typename A<decltype(h.g)>::template C<I<T>>;
17*f4a2713aSLionel Sambuc };
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc A<int> a;
20*f4a2713aSLionel Sambuc A<char>::D<double> b;
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc template<typename T> T make();
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc namespace X {
25*f4a2713aSLionel Sambuc   template<typename T> struct traits {
26*f4a2713aSLionel Sambuc     typedef T thing;
27*f4a2713aSLionel Sambuc     typedef decltype(val(make<thing>())) inner_ptr;
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc     template<typename U> using rebind_thing = typename thing::template rebind<U>;
30*f4a2713aSLionel Sambuc     template<typename U> using rebind = traits<rebind_thing<U>>;
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc     inner_ptr &&alloc();
33*f4a2713aSLionel Sambuc     void free(inner_ptr&&);
34*f4a2713aSLionel Sambuc   };
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   template<typename T> struct ptr_traits {
37*f4a2713aSLionel Sambuc     typedef T *type;
38*f4a2713aSLionel Sambuc   };
39*f4a2713aSLionel Sambuc   template<typename T> using ptr = typename ptr_traits<T>::type;
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc   template<typename T> struct thing {
42*f4a2713aSLionel Sambuc     typedef T inner;
43*f4a2713aSLionel Sambuc     typedef ptr<inner> inner_ptr;
44*f4a2713aSLionel Sambuc     typedef traits<thing<inner>> traits_type;
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc     template<typename U> using rebind = thing<U>;
47*f4a2713aSLionel Sambuc 
thingX::thing48*f4a2713aSLionel Sambuc     thing(traits_type &traits) : traits(traits), val(traits.alloc()) {}
~thingX::thing49*f4a2713aSLionel Sambuc     ~thing() { traits.free(static_cast<inner_ptr&&>(val)); }
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc     traits_type &traits;
52*f4a2713aSLionel Sambuc     inner_ptr val;
53*f4a2713aSLionel Sambuc 
val(const thing & t)54*f4a2713aSLionel Sambuc     friend inner_ptr val(const thing &t) { return t.val; }
55*f4a2713aSLionel Sambuc   };
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   template<> struct ptr_traits<bool> {
58*f4a2713aSLionel Sambuc     typedef bool &type;
59*f4a2713aSLionel Sambuc   };
alloc()60*f4a2713aSLionel Sambuc   template<> bool &traits<thing<bool>>::alloc() { static bool b; return b; }
free(bool &)61*f4a2713aSLionel Sambuc   template<> void traits<thing<bool>>::free(bool&) {}
62*f4a2713aSLionel Sambuc }
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc typedef X::traits<X::thing<int>> itt;
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc itt::thing::traits_type itr;
67*f4a2713aSLionel Sambuc itt::thing ith(itr);
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc itt::rebind<bool> btr;
70*f4a2713aSLionel Sambuc itt::rebind_thing<bool> btt(btr);
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc namespace PR11848 {
73*f4a2713aSLionel Sambuc   template<typename T> using U = int;
74*f4a2713aSLionel Sambuc 
75*f4a2713aSLionel Sambuc   template<typename T, typename ...Ts>
f1(U<T> i,U<Ts>...is)76*f4a2713aSLionel Sambuc   void f1(U<T> i, U<Ts> ...is) { // expected-note 2{{couldn't infer template argument 'T'}}
77*f4a2713aSLionel Sambuc     return i + f1<Ts...>(is...);
78*f4a2713aSLionel Sambuc   }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc   // FIXME: This note is technically correct, but could be better. We
81*f4a2713aSLionel Sambuc   // should really say that we couldn't infer template argument 'Ts'.
82*f4a2713aSLionel Sambuc   template<typename ...Ts>
f2(U<Ts>...is)83*f4a2713aSLionel Sambuc   void f2(U<Ts> ...is) { } // expected-note {{requires 0 arguments, but 1 was provided}}
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc   template<typename...> struct type_tuple {};
86*f4a2713aSLionel Sambuc   template<typename ...Ts>
f3(type_tuple<Ts...>,U<Ts>...is)87*f4a2713aSLionel Sambuc   void f3(type_tuple<Ts...>, U<Ts> ...is) {} // expected-note {{requires 4 arguments, but 3 were provided}}
88*f4a2713aSLionel Sambuc 
g()89*f4a2713aSLionel Sambuc   void g() {
90*f4a2713aSLionel Sambuc     f1(U<void>()); // expected-error {{no match}}
91*f4a2713aSLionel Sambuc     f1(1, 2, 3, 4, 5); // expected-error {{no match}}
92*f4a2713aSLionel Sambuc     f2(); // ok
93*f4a2713aSLionel Sambuc     f2(1); // expected-error {{no match}}
94*f4a2713aSLionel Sambuc     f3(type_tuple<>());
95*f4a2713aSLionel Sambuc     f3(type_tuple<void, void, void>(), 1, 2); // expected-error {{no match}}
96*f4a2713aSLionel Sambuc     f3(type_tuple<void, void, void>(), 1, 2, 3);
97*f4a2713aSLionel Sambuc   }
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc   template<typename ...Ts>
100*f4a2713aSLionel Sambuc   struct S {
101*f4a2713aSLionel Sambuc     S(U<Ts>...ts);
102*f4a2713aSLionel Sambuc   };
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc   template<typename T>
105*f4a2713aSLionel Sambuc   struct Hidden1 {
106*f4a2713aSLionel Sambuc     template<typename ...Ts>
107*f4a2713aSLionel Sambuc     Hidden1(typename T::template U<Ts> ...ts);
108*f4a2713aSLionel Sambuc   };
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc   template<typename T, typename ...Ts>
111*f4a2713aSLionel Sambuc   struct Hidden2 {
112*f4a2713aSLionel Sambuc     Hidden2(typename T::template U<Ts> ...ts);
113*f4a2713aSLionel Sambuc   };
114*f4a2713aSLionel Sambuc 
115*f4a2713aSLionel Sambuc   struct Hide {
116*f4a2713aSLionel Sambuc     template<typename T> using U = int;
117*f4a2713aSLionel Sambuc   };
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc   Hidden1<Hide> h1;
120*f4a2713aSLionel Sambuc   Hidden2<Hide, double, char> h2(1, 2);
121*f4a2713aSLionel Sambuc }
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc namespace Core22036 {
124*f4a2713aSLionel Sambuc   struct X {};
125*f4a2713aSLionel Sambuc   void h(...);
126*f4a2713aSLionel Sambuc   template<typename T> using Y = X;
127*f4a2713aSLionel Sambuc   template<typename T, typename ...Ts> struct S {
128*f4a2713aSLionel Sambuc     // An expression can contain an unexpanded pack without being type or
129*f4a2713aSLionel Sambuc     // value dependent. This is true even if the expression's type is a pack
130*f4a2713aSLionel Sambuc     // expansion type.
f1Core22036::S131*f4a2713aSLionel Sambuc     void f1(Y<T> a) { h(g(a)); } // expected-error {{undeclared identifier 'g'}}
f2Core22036::S132*f4a2713aSLionel Sambuc     void f2(Y<Ts>...as) { h(g(as)...); } // expected-error {{undeclared identifier 'g'}}
f3Core22036::S133*f4a2713aSLionel Sambuc     void f3(Y<Ts>...as) { g(as...); } // ok
f4Core22036::S134*f4a2713aSLionel Sambuc     void f4(Ts ...ts) { h(g(sizeof(ts))...); } // expected-error {{undeclared identifier 'g'}}
135*f4a2713aSLionel Sambuc     // FIXME: We can reject this, since it has no valid instantiations because
136*f4a2713aSLionel Sambuc     // 'g' never has any associated namespaces.
f5Core22036::S137*f4a2713aSLionel Sambuc     void f5(Ts ...ts) { g(sizeof(ts)...); } // ok
138*f4a2713aSLionel Sambuc   };
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc namespace PR13243 {
142*f4a2713aSLionel Sambuc   template<typename A> struct X {};
143*f4a2713aSLionel Sambuc   template<int I> struct C {};
144*f4a2713aSLionel Sambuc   template<int I> using Ci = C<I>;
145*f4a2713aSLionel Sambuc 
f(X<A>,Ci<I>)146*f4a2713aSLionel Sambuc   template<typename A, int I> void f(X<A>, Ci<I>) {}
147*f4a2713aSLionel Sambuc   template void f(X<int>, C<0>);
148*f4a2713aSLionel Sambuc }
149*f4a2713aSLionel Sambuc 
150*f4a2713aSLionel Sambuc namespace PR13136 {
151*f4a2713aSLionel Sambuc   template <typename T, T... Numbers>
152*f4a2713aSLionel Sambuc   struct NumberTuple { };
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc   template <unsigned int... Numbers>
155*f4a2713aSLionel Sambuc   using MyNumberTuple = NumberTuple<unsigned int, Numbers...>;
156*f4a2713aSLionel Sambuc 
157*f4a2713aSLionel Sambuc   template <typename U, unsigned int... Numbers>
158*f4a2713aSLionel Sambuc   void foo(U&&, MyNumberTuple<Numbers...>);
159*f4a2713aSLionel Sambuc 
160*f4a2713aSLionel Sambuc   template <typename U, unsigned int... Numbers>
161*f4a2713aSLionel Sambuc   void bar(U&&, NumberTuple<unsigned int, Numbers...>);
162*f4a2713aSLionel Sambuc 
main()163*f4a2713aSLionel Sambuc   int main() {
164*f4a2713aSLionel Sambuc     foo(1, NumberTuple<unsigned int, 0, 1>());
165*f4a2713aSLionel Sambuc     bar(1, NumberTuple<unsigned int, 0, 1>());
166*f4a2713aSLionel Sambuc     return 0;
167*f4a2713aSLionel Sambuc   }
168*f4a2713aSLionel Sambuc }
169*f4a2713aSLionel Sambuc 
170*f4a2713aSLionel Sambuc namespace PR16646 {
171*f4a2713aSLionel Sambuc   namespace test1 {
172*f4a2713aSLionel Sambuc     template <typename T> struct DefaultValue { const T value=0;};
173*f4a2713aSLionel Sambuc     template <typename ... Args> struct tuple {};
174*f4a2713aSLionel Sambuc     template <typename ... Args> using Zero = tuple<DefaultValue<Args> ...>;
175*f4a2713aSLionel Sambuc     template <typename ... Args> void f(const Zero<Args ...> &t);
f()176*f4a2713aSLionel Sambuc     void f() {
177*f4a2713aSLionel Sambuc         f(Zero<int,double,double>());
178*f4a2713aSLionel Sambuc     }
179*f4a2713aSLionel Sambuc   }
180*f4a2713aSLionel Sambuc 
181*f4a2713aSLionel Sambuc   namespace test2 {
182*f4a2713aSLionel Sambuc     template<int x> struct X {};
183*f4a2713aSLionel Sambuc     template <template<int x> class temp> struct DefaultValue { const temp<0> value; };
184*f4a2713aSLionel Sambuc     template <typename ... Args> struct tuple {};
185*f4a2713aSLionel Sambuc     template <template<int x> class... Args> using Zero = tuple<DefaultValue<Args> ...>;
186*f4a2713aSLionel Sambuc     template <template<int x> class... Args> void f(const Zero<Args ...> &t);
f()187*f4a2713aSLionel Sambuc     void f() {
188*f4a2713aSLionel Sambuc       f(Zero<X,X,X>());
189*f4a2713aSLionel Sambuc     }
190*f4a2713aSLionel Sambuc   }
191*f4a2713aSLionel Sambuc }
192*f4a2713aSLionel Sambuc 
193*f4a2713aSLionel Sambuc namespace PR16904 {
194*f4a2713aSLionel Sambuc   template <typename,typename>
195*f4a2713aSLionel Sambuc   struct base {
196*f4a2713aSLionel Sambuc     template <typename> struct derived;
197*f4a2713aSLionel Sambuc   };
198*f4a2713aSLionel Sambuc   // FIXME: The diagnostics here are terrible.
199*f4a2713aSLionel Sambuc   template <typename T, typename U, typename V>
200*f4a2713aSLionel Sambuc   using derived = base<T, U>::template derived<V>; // expected-error {{expected a type}} expected-error {{expected ';'}}
201*f4a2713aSLionel Sambuc   template <typename T, typename U, typename V>
202*f4a2713aSLionel Sambuc   using derived2 = ::PR16904::base<T, U>::template derived<V>; // expected-error {{expected a type}} expected-error {{expected ';'}}
203*f4a2713aSLionel Sambuc }
204