xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/alias-template.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -verify -std=c++11 %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc namespace RedeclAliasTypedef {
4f4a2713aSLionel Sambuc   template<typename U> using T = int;
5f4a2713aSLionel Sambuc   template<typename U> using T = int;
6f4a2713aSLionel Sambuc   template<typename U> using T = T<U>;
7f4a2713aSLionel Sambuc }
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc namespace IllegalTypeIds {
10f4a2713aSLionel Sambuc   template<typename U> using A = void(int n = 0); // expected-error {{default arguments can only be specified for parameters in a function declaration}}
11f4a2713aSLionel Sambuc   template<typename U> using B = inline void(int n); // expected-error {{type name does not allow function specifier}}
12f4a2713aSLionel Sambuc   template<typename U> using C = virtual void(int n); // expected-error {{type name does not allow function specifier}}
13f4a2713aSLionel Sambuc   template<typename U> using D = explicit void(int n); // expected-error {{type name does not allow function specifier}}
14f4a2713aSLionel Sambuc   template<typename U> using E = void(int n) throw(); // expected-error {{exception specifications are not allowed in type aliases}}
15f4a2713aSLionel Sambuc   template<typename U> using F = void(*)(int n) &&; // expected-error {{pointer to function type cannot have '&&' qualifier}}
16f4a2713aSLionel Sambuc   template<typename U> using G = __thread void(int n); // expected-error {{type name does not allow storage class to be specified}}
17f4a2713aSLionel Sambuc   template<typename U> using H = constexpr int; // expected-error {{type name does not allow constexpr specifier}}
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc   template<typename U> using Y = void(int n); // ok
20f4a2713aSLionel Sambuc   template<typename U> using Z = void(int n) &&; // ok
21f4a2713aSLionel Sambuc }
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc namespace IllegalSyntax {
24f4a2713aSLionel Sambuc   template<typename Z> using ::T = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
25f4a2713aSLionel Sambuc   template<typename Z> using operator int = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
26f4a2713aSLionel Sambuc   template<typename Z> using typename U = void; // expected-error {{name defined in alias declaration must be an identifier}}
27f4a2713aSLionel Sambuc   template<typename Z> using typename ::V = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
28f4a2713aSLionel Sambuc   template<typename Z> using typename ::operator bool = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
29f4a2713aSLionel Sambuc }
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc namespace VariableLengthArrays {
32f4a2713aSLionel Sambuc   template<typename Z> using T = int[42]; // ok
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc   int n = 32;
35f4a2713aSLionel Sambuc   template<typename Z> using T = int[n]; // expected-error {{variable length array declaration not allowed at file scope}}
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc   const int m = 42;
38f4a2713aSLionel Sambuc   template<typename Z> using U = int[m]; // expected-note {{previous definition}}
39f4a2713aSLionel Sambuc   template<typename Z> using U = int[42]; // ok
40f4a2713aSLionel Sambuc   template<typename Z> using U = int; // expected-error {{type alias template redefinition with different types ('int' vs 'int [42]')}}
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc namespace RedeclFunc {
44f4a2713aSLionel Sambuc   int f(int, char**);
45f4a2713aSLionel Sambuc   template<typename Z> using T = int;
46f4a2713aSLionel Sambuc   T<char> f(int, char **); // ok
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc namespace LookupFilter {
50f4a2713aSLionel Sambuc   namespace N { template<typename U> using S = int; }
51f4a2713aSLionel Sambuc   using namespace N;
52f4a2713aSLionel Sambuc   template<typename U> using S = S<U>*; // ok
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc namespace InFunctions {
56f4a2713aSLionel Sambuc   template<typename...T> struct S0 {
57f4a2713aSLionel Sambuc     template<typename Z> using U = T*; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
58f4a2713aSLionel Sambuc     U<char> u;
59f4a2713aSLionel Sambuc   };
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc   template<typename Z> using T1 = int;
62f4a2713aSLionel Sambuc   template<typename Z> using T2 = int[-1]; // expected-error {{array size is negative}}
63f4a2713aSLionel Sambuc   template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
64f4a2713aSLionel Sambuc     template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
65f4a2713aSLionel Sambuc   };
66f4a2713aSLionel Sambuc   template<typename Z> using Z = Z;
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc namespace ClassNameRedecl {
70f4a2713aSLionel Sambuc   class C0 {
71f4a2713aSLionel Sambuc     // FIXME: this diagnostic is pretty poor
72f4a2713aSLionel Sambuc     template<typename U> using C0 = int; // expected-error {{name defined in alias declaration must be an identifier}}
73f4a2713aSLionel Sambuc   };
74f4a2713aSLionel Sambuc   class C1 {
75f4a2713aSLionel Sambuc     // FIXME: this diagnostic is pretty poor
76f4a2713aSLionel Sambuc     template<typename U> using C1 = C1; // expected-error {{name defined in alias declaration must be an identifier}}
77f4a2713aSLionel Sambuc   };
78f4a2713aSLionel Sambuc   class C2 {
79f4a2713aSLionel Sambuc     template<typename U> using C0 = C1; // ok
80f4a2713aSLionel Sambuc   };
81f4a2713aSLionel Sambuc   template<typename...T> class C3 {
82f4a2713aSLionel Sambuc     template<typename U> using f = T; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
83f4a2713aSLionel Sambuc   };
84f4a2713aSLionel Sambuc   template<typename T> class C4 { // expected-note {{template parameter is declared here}}
85f4a2713aSLionel Sambuc     template<typename U> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
86f4a2713aSLionel Sambuc   };
87f4a2713aSLionel Sambuc   class C5 {
88f4a2713aSLionel Sambuc     class c; // expected-note {{previous definition}}
89f4a2713aSLionel Sambuc     template<typename U> using c = int; // expected-error {{redefinition of 'c' as different kind of symbol}}
90f4a2713aSLionel Sambuc     class d; // expected-note {{previous definition}}
91f4a2713aSLionel Sambuc     template<typename U> using d = d; // expected-error {{redefinition of 'd' as different kind of symbol}}
92f4a2713aSLionel Sambuc   };
93f4a2713aSLionel Sambuc   class C6 {
94f4a2713aSLionel Sambuc     class c { template<typename U> using C6 = int; }; // ok
95f4a2713aSLionel Sambuc   };
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc 
98f4a2713aSLionel Sambuc class CtorDtorName {
99f4a2713aSLionel Sambuc   template<typename T> using X = CtorDtorName;
100f4a2713aSLionel Sambuc   X<int>(); // expected-error {{expected member name}}
101f4a2713aSLionel Sambuc   ~X<int>(); // expected-error {{destructor cannot be declared using a type alias}}
102f4a2713aSLionel Sambuc };
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc namespace TagName {
105f4a2713aSLionel Sambuc   template<typename Z> using S = struct { int n; }; // expected-error {{cannot be defined}}
106f4a2713aSLionel Sambuc   template<typename Z> using T = class { int n; }; // expected-error {{cannot be defined}}
107f4a2713aSLionel Sambuc   template<typename Z> using U = enum { a, b, c }; // expected-error {{cannot be defined}}
108f4a2713aSLionel Sambuc   template<typename Z> using V = struct V { int n; }; // expected-error {{'TagName::V' cannot be defined in a type alias template}}
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc namespace StdExample {
112f4a2713aSLionel Sambuc   template<typename T, typename U> struct pair;
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   template<typename T> using handler_t = void (*)(T);
115f4a2713aSLionel Sambuc   extern handler_t<int> ignore;
116f4a2713aSLionel Sambuc   extern void (*ignore)(int);
117f4a2713aSLionel Sambuc   // FIXME: we recover as if cell is an undeclared variable. the diagnostics are terrible!
118f4a2713aSLionel Sambuc   template<typename T> using cell = pair<T*, cell<T>*>; // expected-error {{use of undeclared identifier 'cell'}} \
119f4a2713aSLionel Sambuc                                                            expected-error {{'T' does not refer to a value}} \
120f4a2713aSLionel Sambuc                                                            expected-note {{declared here}} \
121f4a2713aSLionel Sambuc                                                            expected-error {{expected ';' after alias declaration}}
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc namespace Access {
125f4a2713aSLionel Sambuc   class C0 {
126f4a2713aSLionel Sambuc     template<typename Z> using U = int; // expected-note {{declared private here}}
127f4a2713aSLionel Sambuc   };
128f4a2713aSLionel Sambuc   C0::U<int> v; // expected-error {{'U' is a private member}}
129f4a2713aSLionel Sambuc   class C1 {
130f4a2713aSLionel Sambuc   public:
131f4a2713aSLionel Sambuc     template<typename Z> using U = int;
132f4a2713aSLionel Sambuc   };
133f4a2713aSLionel Sambuc   C1::U<int> w; // ok
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc namespace VoidArg {
137f4a2713aSLionel Sambuc   template<typename Z> using V = void;
138f4a2713aSLionel Sambuc   V<int> f(int); // ok
139*0a6a1f1dSLionel Sambuc   V<char> g(V<double>); // ok (DR577)
140f4a2713aSLionel Sambuc }
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc namespace Curried {
143f4a2713aSLionel Sambuc   template<typename T, typename U> struct S;
144f4a2713aSLionel Sambuc   template<typename T> template<typename U> using SS = S<T, U>; // expected-error {{extraneous template parameter list in alias template declaration}}
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc // PR12647
148f4a2713aSLionel Sambuc namespace SFINAE {
149f4a2713aSLionel Sambuc   template<bool> struct enable_if; // expected-note 2{{here}}
150f4a2713aSLionel Sambuc   template<> struct enable_if<true> { using type = void; };
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc   template<typename T> struct is_enum { static constexpr bool value = __is_enum(T); };
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc   template<typename T> using EnableIf = typename enable_if<T::value>::type; // expected-error {{undefined template}}
155f4a2713aSLionel Sambuc   template<typename T> using DisableIf = typename enable_if<!T::value>::type; // expected-error {{undefined template}}
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   template<typename T> EnableIf<is_enum<T>> f();
158f4a2713aSLionel Sambuc   template<typename T> DisableIf<is_enum<T>> f();
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc   enum E { e };
161f4a2713aSLionel Sambuc 
main()162f4a2713aSLionel Sambuc   int main() {
163f4a2713aSLionel Sambuc     f<int>();
164f4a2713aSLionel Sambuc     f<E>();
165f4a2713aSLionel Sambuc   }
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc   template<typename T, typename U = EnableIf<is_enum<T>>> struct fail1 {}; // expected-note {{here}}
168f4a2713aSLionel Sambuc   template<typename T> struct fail2 : DisableIf<is_enum<T>> {}; // expected-note {{here}}
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc   fail1<int> f1; // expected-note {{here}}
171f4a2713aSLionel Sambuc   fail2<E> f2; // expected-note {{here}}
172f4a2713aSLionel Sambuc }
173