Debian Codegraph @

clang/test/SemaCXX/alias-template.cpp

raw
1 // RUN: %clang_cc1 -verify -std=c++14 -fcxx-exceptions %s
2
3 namespace RedeclAliasTypedef {
4 template<typename U> using T = int;
5 template<typename U> using T = int;
6 template<typename U> using T = T<U>;
7 }
8
9 namespace IllegalTypeIds {
10 template<typename U> using A = void(int n = 0); // expected-error {{default arguments can only be specified for parameters in a function declaration}}
11 template<typename U> using B = inline void(int n); // expected-error {{type name does not allow function specifier}}
12 template<typename U> using C = virtual void(int n); // expected-error {{type name does not allow function specifier}}
13 template<typename U> using D = explicit void(int n); // expected-error {{type name does not allow function specifier}}
14 template<typename U> using E = void(int n) throw(); // expected-error {{exception specifications are not allowed in type aliases}}
15 template<typename U> using F = void(*)(int n) &&; // expected-error {{pointer to function type cannot have '&&' qualifier}}
16 template<typename U> using G = __thread void(int n); // expected-error {{type name does not allow storage class to be specified}}
17 template<typename U> using H = constexpr int; // expected-error {{type name does not allow constexpr specifier}}
18
19 template<typename U> using Y = void(int n); // ok
20 template<typename U> using Z = void(int n) &&; // ok
21 }
22
23 namespace IllegalSyntax {
24 template<typename Z> using ::T = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
25 template<typename Z> using operator int = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
26 template<typename Z> using typename U = void; // expected-error {{name defined in alias declaration must be an identifier}}
27 template<typename Z> using typename ::V = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
28 template<typename Z> using typename ::operator bool = void(int n); // expected-error {{name defined in alias declaration must be an identifier}}
29 }
30
31 namespace VariableLengthArrays {
32 template<typename Z> using T = int[42]; // ok
33
34 int n = 32; // expected-note {{declared here}}
35 template<typename Z> using T = int[n]; // expected-error {{variable length array declaration not allowed at file scope}} \
36 expected-warning {{variable length arrays in C++ are a Clang extension}} \
37 expected-note {{read of non-const variable 'n' is not allowed in a constant expression}}
38
39 const int m = 42;
40 template<typename Z> using U = int[m];
41 template<typename Z> using U = int[42]; // expected-note {{previous definition}}
42 template<typename Z> using U = int; // expected-error {{type alias template redefinition with different types ('int' vs 'int[42]')}}
43 }
44
45 namespace RedeclFunc {
46 int f(int, char**);
47 template<typename Z> using T = int;
48 T<char> f(int, char **); // ok
49 }
50
51 namespace LookupFilter {
52 namespace N { template<typename U> using S = int; }
53 using namespace N;
54 template<typename U> using S = S<U>*; // ok
55 }
56
57 namespace UnexpandedPack {
58 template<typename...T> struct S0 {
59 template<typename Z> using U = T*; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
60 U<char> u;
61 };
62 }
63
64 namespace InvalidType {
65 template<typename Z> using T1 = int;
66 template<typename Z> using T2 = int[-1]; // expected-error {{array size is negative}}
67 }
68
69 namespace ShadowTemplateParam {
70 template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
71 template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
72 };
73 template<typename Z> // expected-note {{template parameter is declared here}}
74 using Z = Z; // expected-error {{declaration of 'Z' shadows template parameter}}
75 }
76
77 namespace ClassNameRedecl {
78 class C0 {
79 template<typename U> using C0 = int; // expected-error {{member 'C0' has the same name as its class}}
80 };
81 class C1 {
82 template<typename U> using C1 = C1; // expected-error {{member 'C1' has the same name as its class}}
83 };
84 class C2 {
85 template<typename U> using C0 = C1; // ok
86 };
87 template<typename...T> class C3 {
88 template<typename U> using f = T; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
89 };
90 template<typename T> class C4 { // expected-note {{template parameter is declared here}}
91 template<typename U> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
92 };
93 class C5 {
94 class c; // expected-note {{previous definition}}
95 template<typename U> using c = int; // expected-error {{redefinition of 'c' as different kind of symbol}}
96 class d; // expected-note {{previous definition}}
97 template<typename U> using d = d; // expected-error {{redefinition of 'd' as different kind of symbol}}
98 };
99 class C6 {
100 class c { template<typename U> using C6 = int; }; // ok
101 };
102 }
103
104 class CtorDtorName {
105 template<typename T> using X = CtorDtorName;
106 X<int>(); // expected-error {{expected member name}}
107 ~X<int>(); // expected-error {{destructor cannot be declared using a type alias}}
108 };
109
110 namespace TagName {
111 template<typename Z> using S = struct { int n; }; // expected-error {{cannot be defined}}
112 template<typename Z> using T = class { int n; }; // expected-error {{cannot be defined}}
113 template<typename Z> using U = enum { a, b, c }; // expected-error {{cannot be defined}}
114 template<typename Z> using V = struct V { int n; }; // expected-error {{'TagName::V' cannot be defined in a type alias template}}
115 }
116
117 namespace StdExample {
118 template<typename T, typename U> struct pair;
119
120 template<typename T> using handler_t = void (*)(T);
121 extern handler_t<int> ignore;
122 extern void (*ignore)(int);
123 // FIXME: we recover as if cell is an undeclared variable template
124 template<typename T> using cell = pair<T*, cell<T>*>; // expected-error {{use of undeclared identifier 'cell'}} expected-error {{expected expression}}
125 }
126
127 namespace Access {
128 class C0 {
129 template<typename Z> using U = int; // expected-note {{declared private here}}
130 };
131 C0::U<int> v; // expected-error {{'U' is a private member}}
132 class C1 {
133 public:
134 template<typename Z> using U = int;
135 };
136 C1::U<int> w; // ok
137 }
138
139 namespace VoidArg {
140 template<typename Z> using V = void;
141 V<int> f(int); // ok
142 V<char> g(V<double>); // ok (DR577)
143 }
144
145 namespace Curried {
146 template<typename T, typename U> struct S;
147 template<typename T> template<typename U> using SS = S<T, U>; // expected-error {{extraneous template parameter list in alias template declaration}}
148 }
149
150 // PR12647
151 namespace SFINAE {
152 template<bool> struct enable_if; // expected-note 2{{here}}
153 template<> struct enable_if<true> { using type = void; };
154
155 template<typename T> struct is_enum { static constexpr bool value = __is_enum(T); };
156
157 template<typename T> using EnableIf = typename enable_if<T::value>::type; // expected-error {{undefined template}}
158 template<typename T> using DisableIf = typename enable_if<!T::value>::type; // expected-error {{undefined template}}
159
160 template<typename T> EnableIf<is_enum<T>> f();
161 template<typename T> DisableIf<is_enum<T>> f();
162
163 enum E { e };
164
165 int main() {
166 f<int>();
167 f<E>();
168 }
169
170 template<typename T, typename U = EnableIf<is_enum<T>>> struct fail1 {}; // expected-note {{here}}
171 template<typename T> struct fail2 : DisableIf<is_enum<T>> {}; // expected-note {{here}}
172
173 fail1<int> f1; // expected-note {{here}}
174 fail2<E> f2; // expected-note {{here}}
175 }
176
177 namespace PR24212 {
178 struct X {};
179 template <int I>
180 struct S {
181 template <int J>
182 using T = X[J];
183 using U = T<I>;
184 };
185 static_assert(__is_same(S<3>::U, X[2]), ""); // expected-error {{static assertion failed}}
186 }
187
188 namespace PR39623 {
189 template <class T>
190 using void_t = void;
191
192 template <class T, class = void_t<typename T::wait_what>>
193 int sfinae_me() { return 0; } // expected-note{{candidate template ignored: substitution failure}}
194
195 int g = sfinae_me<int>(); // expected-error{{no matching function for call to 'sfinae_me'}}
196 }
197
198 namespace NullExceptionDecl {
199 template<int... I> auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}}
200 }