Debian Codegraph @

clang/test/SemaCXX/decltype.cpp

raw
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wno-c99-designator %s
2 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -Wno-c99-designator %s
3
4 // PR5290
5 int const f0();
6 void f0_test() {
7 decltype(0, f0()) i = 0;
8 i = 0;
9 }
10
11 struct A { int a[1]; A() { } };
12 typedef A const AC;
13 int &f1(int*);
14 float &f2(int const*);
15
16 void test_f2() {
17 float &fr = f2(AC().a);
18 }
19
20 template <class T>
21 struct Future {
22 explicit Future(T v);
23
24 template <class F>
25 auto call(F&& fn) -> decltype(fn(T())) {
26 return fn(T());
27 }
28
29 template <class B, class F>
30 auto then(F&& fn) -> decltype(call(fn))
31 {
32 return fn(T());
33 }
34 };
35
36 void rdar16527205() {
37 Future<int> f1(42);
38 f1.call([](int){ return Future<float>(0); });
39 }
40
41 namespace pr10154 {
42 class A{
43 A(decltype(nullptr) param);
44 };
45 }
46
47 template<typename T> struct S {};
48 template<typename T> auto f(T t) -> decltype(S<int>(t)) {
49 using U = decltype(S<int>(t));
50 using U = S<int>;
51 return S<int>(t);
52 }
53
54 struct B {
55 B(decltype(undeclared)); // expected-error {{undeclared identifier}}
56 };
57 struct C {
58 C(decltype(undeclared; // expected-error {{undeclared identifier}} \
59 // expected-error {{expected ')'}} expected-note {{to match this '('}}
60 };
61
62 namespace PR16529 {
63 struct U {};
64 template <typename T> struct S {
65 static decltype(T{}, U{}) &f();
66 };
67 U &r = S<int>::f();
68 }
69
70 namespace PR18876 {
71 struct A { ~A() = delete; }; // expected-note +{{here}}
72 A f();
73 decltype(f()) *a; // ok, function call
74 decltype(A()) *b; // expected-error {{attempt to use a deleted function}}
75 decltype(0, f()) *c; // ok, function call on RHS of comma
76 decltype(0, A()) *d; // expected-error {{attempt to use a deleted function}}
77 decltype(f(), 0) *e; // expected-error {{attempt to use a deleted function}}
78 }
79
80 namespace D5789 {
81 struct P1 { char x[6]; } g1 = { "foo" };
82 struct LP1 { struct P1 p1; };
83
84 template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {} // expected-note {{previous definition is here}}
85
86 template<class T>
87 void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'r' }))) {} // okay
88
89 template<class T>
90 void foo(decltype(T(LP1{ .p1 = { "foo" }, .p1.x[1] = 'x'}))) {} // okay
91
92 // expected-error@+1 {{redefinition of 'foo'}}
93 template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {}
94 }
95
96 namespace GH58674 {
97 struct Foo {
98 float value_;
99 struct nested {
100 float value_;
101 };
102 };
103
104 template <typename T>
105 struct TemplateFoo {
106 float value_;
107 };
108
109 float bar;
110
111 template <typename T>
112 struct Animal{};
113
114 template <typename T>
115 class Cat : Animal<T> {
116 using okay = decltype(Foo::value_);
117 using also_okay = decltype(bar);
118 using okay2 = decltype(Foo::nested::value_);
119 using okay3 = decltype(TemplateFoo<T>::value_);
120 public:
121 void meow() {
122 using okay = decltype(Foo::value_);
123 using also_okay = decltype(bar);
124 using okay2 = decltype(Foo::nested::value_);
125 using okay3 = decltype(TemplateFoo<T>::value_);
126 }
127 };
128
129 void baz() {
130 Cat<void>{}.meow();
131 }
132 }
133
134 namespace GH97646 {
135 template<bool B>
136 void f() {
137 decltype(B) x = false;
138 !x;
139 }
140 }
141
142 namespace GH99873 {
143 struct B {
144 int x;
145 };
146
147 template<typename T>
148 struct A {
149 template<typename U>
150 constexpr int f() const {
151 return 1;
152 }
153
154 template<>
155 constexpr int f<int>() const {
156 return decltype(B::x)();
157 }
158 };
159
160
161
162 // This shouldn't crash.
163 static_assert(A<int>().f<int>() == 0, "");
164 // The result should not be dependent.
165 static_assert(A<int>().f<int>() != 0, ""); // expected-error {{static assertion failed due to requirement 'GH99873::A<int>().f<int>() != 0'}}
166 // expected-note@-1 {{expression evaluates to '0 != 0'}}
167 }
168
169
170 #if __cplusplus >= 201703L
171 namespace GH160497 {
172
173 template <class> struct S {
174 template <class>
175 inline static auto mem =
176 [] { static_assert(false); // expected-error {{static assertion failed}} \
177 // expected-note {{while substituting into a lambda expression here}}
178 return 42;
179 }();
180 };
181
182 using T = decltype(S<void>::mem<void>);
183 // expected-note@-1 {{in instantiation of static data member 'GH160497::S<void>::mem<void>' requested here}}
184
185
186 template <class> struct S2 {
187 template <class>
188 inline static auto* mem =
189 [] { static_assert(false); // expected-error {{static assertion failed}} \
190 // expected-note {{while substituting into a lambda expression here}}
191 return static_cast<int*>(nullptr);
192 }();
193 };
194
195 using T2 = decltype(S2<void>::mem<void>);
196 //expected-note@-1 {{in instantiation of static data member 'GH160497::S2<void>::mem<void>' requested here}}
197
198 template <class> struct S3 {
199 template <class>
200 inline static int mem = // Check we don't instantiate when the type is not deduced.
201 [] { static_assert(false);
202 return 42;
203 }();
204 };
205
206 using T = decltype(S3<void>::mem<void>);
207 }
208
209 namespace N1 {
210
211 template<class>
212 struct S {
213 template<class>
214 inline static auto mem = 42;
215 };
216
217 using T = decltype(S<void>::mem<void>);
218
219 T y = 42;
220
221 }
222
223 namespace GH161196 {
224
225 template <typename> struct A {
226 static constexpr int digits = 0;
227 };
228
229 template <typename> struct B {
230 template <int, typename MaskInt = int, int = A<MaskInt>::digits>
231 static constexpr auto XBitMask = 0;
232 };
233
234 struct C {
235 using ReferenceHost = B<int>;
236 template <int> static decltype(ReferenceHost::XBitMask<0>) XBitMask;
237 };
238
239 void test() { (void)C::XBitMask<0>; }
240
241 }
242 #endif
243
244 template<typename>
245 class conditional {
246 };
247
248 // FIXME: The diagnostics here are produced twice.
249 void foo(conditional<decltype((1),int>) { // expected-note 2 {{to match this '('}} expected-error {{expected ')'}} expected-note 2{{to match this '<'}}
250 } // expected-error {{expected function body after function declarator}} expected-error 2 {{expected '>'}} expected-error {{expected ')'}}