1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++2a -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
6
7 #if __cplusplus < 201103L
8 // expected-error@+1 {{variadic macro}}
9 #define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)
10 #endif
11
12 #if __cplusplus >= 201103L
13 namespace std {
14 typedef decltype(sizeof(int)) size_t;
15
16 template<typename E> class initializer_list {
17 const E *begin;
18 size_t size;
19
20 public:
21 initializer_list();
22 };
23 } // std
24 #endif
25
26 namespace dr1601 { // dr1601: 10
27 enum E : char { e };
28 #if __cplusplus < 201103L
29 // expected-error@-2 {{enumeration types with a fixed underlying type are a C++11 extension}}
30 #endif
31 void f(char);
32 void f(int);
g()33 void g() {
34 f(e);
35 }
36 } // namespace dr1601
37
38 namespace dr1611 { // dr1611: dup 1658
39 struct A { A(int); };
40 struct B : virtual A { virtual void f() = 0; };
Cdr1611::C41 struct C : B { C() : A(0) {} void f(); };
42 C c;
43 }
44
45 namespace dr1684 { // dr1684: 3.6
46 #if __cplusplus >= 201103L
47 struct NonLiteral { // expected-note {{because}}
48 NonLiteral();
fdr1684::NonLiteral49 constexpr int f() { return 0; } // expected-warning 0-1{{will not be implicitly 'const'}}
50 };
f(NonLiteral &)51 constexpr int f(NonLiteral &) { return 0; }
f(NonLiteral)52 constexpr int f(NonLiteral) { return 0; } // expected-error {{not a literal type}}
53 #endif
54 }
55
56 namespace dr1631 { // dr1631: 3.7
57 #if __cplusplus >= 201103L
58 // Incorrect overload resolution for single-element initializer-list
59
60 struct A { int a[1]; };
61 struct B { B(int); };
62 void f(B, int);
63 void f(B, int, int = 0);
64 void f(int, A);
65
test()66 void test() {
67 f({0}, {{1}}); // expected-warning {{braces around scalar init}}
68 }
69
70 namespace with_error {
71 void f(B, int); // TODO: expected- note {{candidate function}}
72 void f(int, A); // expected-note {{candidate function}}
73 void f(int, A, int = 0); // expected-note {{candidate function}}
74
test()75 void test() {
76 f({0}, {{1}}); // expected-error{{call to 'f' is ambiguous}}
77 }
78 }
79 #endif
80 }
81
82 namespace dr1638 { // dr1638: yes
83 #if __cplusplus >= 201103L
84 template<typename T> struct A {
85 enum class E; // expected-note {{previous}}
86 enum class F : T; // expected-note 2{{previous}}
87 };
88
89 template<> enum class A<int>::E;
90 template<> enum class A<int>::E {};
91 template<> enum class A<int>::F : int;
92 template<> enum class A<int>::F : int {};
93
94 template<> enum class A<short>::E : int;
95 template<> enum class A<short>::E : int {};
96
97 template<> enum class A<short>::F; // expected-error {{different underlying type}}
98 template<> enum class A<char>::E : char; // expected-error {{different underlying type}}
99 template<> enum class A<char>::F : int; // expected-error {{different underlying type}}
100
101 enum class A<unsigned>::E; // expected-error {{template specialization requires 'template<>'}} expected-error {{nested name specifier}}
102 template enum class A<unsigned>::E; // expected-error {{enumerations cannot be explicitly instantiated}}
103 enum class A<unsigned>::E *e; // expected-error {{must use 'enum' not 'enum class'}}
104
105 struct B {
106 friend enum class A<unsigned>::E; // expected-error {{must use 'enum' not 'enum class'}}
107 };
108 #endif
109 }
110
111 namespace dr1645 { // dr1645: 3.9
112 #if __cplusplus >= 201103L
113 struct A {
114 constexpr A(int, float = 0); // expected-note {{candidate}}
115 explicit A(int, int = 0); // expected-note 2{{candidate}}
116 A(int, int, int = 0) = delete; // expected-note {{candidate}}
117 };
118
119 struct B : A {
120 using A::A; // expected-note 4{{inherited here}}
121 };
122
123 constexpr B a(0); // expected-error {{ambiguous}}
124 constexpr B b(0, 0); // expected-error {{ambiguous}}
125 #endif
126 }
127
128 namespace dr1653 { // dr1653: 4 c++17
f(bool b)129 void f(bool b) {
130 ++b;
131 b++;
132 #if __cplusplus <= 201402L
133 // expected-warning@-3 {{deprecated}} expected-warning@-2 {{deprecated}}
134 #else
135 // expected-error@-5 {{incrementing expression of type bool}} expected-error@-4 {{incrementing expression of type bool}}
136 #endif
137 --b; // expected-error {{cannot decrement expression of type bool}}
138 b--; // expected-error {{cannot decrement expression of type bool}}
139 b += 1; // ok
140 b -= 1; // ok
141 }
142 }
143
144 namespace dr1658 { // dr1658: 5
145 namespace DefCtor {
146 class A { A(); }; // expected-note 0-2{{here}}
147 class B { ~B(); }; // expected-note 0-2{{here}}
148
149 // The stars align! An abstract class does not construct its virtual bases.
150 struct C : virtual A { C(); virtual void foo() = 0; };
151 C::C() = default; // ok, not deleted, expected-error 0-1{{extension}}
152 struct D : virtual B { D(); virtual void foo() = 0; };
153 D::D() = default; // ok, not deleted, expected-error 0-1{{extension}}
154
155 // In all other cases, we are not so lucky.
156 struct E : A { E(); virtual void foo() = 0; };
157 #if __cplusplus < 201103L
158 E::E() = default; // expected-error {{private default constructor}} expected-error {{extension}} expected-note {{here}}
159 #else
160 E::E() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible default constructor}}
161 #endif
162 struct F : virtual A { F(); };
163 #if __cplusplus < 201103L
164 F::F() = default; // expected-error {{private default constructor}} expected-error {{extension}} expected-note {{here}}
165 #else
166 F::F() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible default constructor}}
167 #endif
168
169 struct G : B { G(); virtual void foo() = 0; };
170 #if __cplusplus < 201103L
171 G::G() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
172 #else
173 G::G() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
174 #endif
175 struct H : virtual B { H(); };
176 #if __cplusplus < 201103L
177 H::H() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
178 #else
179 H::H() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
180 #endif
181 }
182
183 namespace Dtor {
184 class B { ~B(); }; // expected-note 0-2{{here}}
185
186 struct D : virtual B { ~D(); virtual void foo() = 0; };
187 D::~D() = default; // ok, not deleted, expected-error 0-1{{extension}}
188
189 struct G : B { ~G(); virtual void foo() = 0; };
190 #if __cplusplus < 201103L
191 G::~G() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
192 #else
193 G::~G() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
194 #endif
195 struct H : virtual B { ~H(); };
196 #if __cplusplus < 201103L
197 H::~H() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
198 #else
199 H::~H() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
200 #endif
201 }
202
203 namespace MemInit {
204 struct A { A(int); }; // expected-note {{here}}
205 struct B : virtual A {
Bdr1658::MemInit::B206 B() {}
207 virtual void f() = 0;
208 };
209 struct C : virtual A {
Cdr1658::MemInit::C210 C() {} // expected-error {{must explicitly initialize}}
211 };
212 }
213
214 namespace CopyCtorParamType {
215 struct A { A(A&); };
216 struct B : virtual A { virtual void f() = 0; };
217 struct C : virtual A { virtual void f(); };
218 struct D : A { virtual void f() = 0; };
219
220 struct X {
221 friend B::B(const B&) throw();
222 friend C::C(C&);
223 friend D::D(D&);
224 };
225 }
226
227 namespace CopyCtor {
228 class A { A(const A&); A(A&&); }; // expected-note 0-4{{here}} expected-error 0-1{{extension}}
229
230 struct C : virtual A { C(const C&); C(C&&); virtual void foo() = 0; }; // expected-error 0-1{{extension}}
231 C::C(const C&) = default; // expected-error 0-1{{extension}}
232 C::C(C&&) = default; // expected-error 0-2{{extension}}
233
234 struct E : A { E(const E&); E(E&&); virtual void foo() = 0; }; // expected-error 0-1{{extension}}
235 #if __cplusplus < 201103L
236 E::E(const E&) = default; // expected-error {{private copy constructor}} expected-error {{extension}} expected-note {{here}}
237 E::E(E&&) = default; // expected-error {{private move constructor}} expected-error 2{{extension}} expected-note {{here}}
238 #else
239 E::E(const E&) = default; // expected-error {{would delete}} expected-note@-5{{inaccessible copy constructor}}
240 E::E(E&&) = default; // expected-error {{would delete}} expected-note@-6{{inaccessible move constructor}}
241 #endif
242 struct F : virtual A { F(const F&); F(F&&); }; // expected-error 0-1{{extension}}
243 #if __cplusplus < 201103L
244 F::F(const F&) = default; // expected-error {{private copy constructor}} expected-error {{extension}} expected-note {{here}}
245 F::F(F&&) = default; // expected-error {{private move constructor}} expected-error 2{{extension}} expected-note {{here}}
246 #else
247 F::F(const F&) = default; // expected-error {{would delete}} expected-note@-5{{inaccessible copy constructor}}
248 F::F(F&&) = default; // expected-error {{would delete}} expected-note@-6{{inaccessible move constructor}}
249 #endif
250 }
251
252 // assignment case is superseded by dr2180
253 }
254
255 namespace dr1672 { // dr1672: 7
256 struct Empty {};
257 struct A : Empty {};
258 struct B { Empty e; };
259 struct C : A { B b; int n; };
260 struct D : A { int n; B b; };
261
262 static_assert(!__is_standard_layout(C), "");
263 static_assert(__is_standard_layout(D), "");
264
265 struct E { B b; int n; };
266 struct F { int n; B b; };
267 union G { B b; int n; };
268 union H { int n; B b; };
269
270 struct X {};
271 template<typename T> struct Y : X, A { T t; };
272
273 static_assert(!__is_standard_layout(Y<E>), "");
274 static_assert(__is_standard_layout(Y<F>), "");
275 static_assert(!__is_standard_layout(Y<G>), "");
276 static_assert(!__is_standard_layout(Y<H>), "");
277 static_assert(!__is_standard_layout(Y<X>), "");
278 }
279
280 namespace dr1687 { // dr1687: 7
281 template<typename T> struct To {
282 operator T(); // expected-note 2{{first operand was implicitly converted to type 'int *'}}
283 // expected-note@-1 {{second operand was implicitly converted to type 'double'}}
284 #if __cplusplus > 201703L
285 // expected-note@-3 2{{operand was implicitly converted to type 'dr1687::E}}
286 #endif
287 };
288
289 int *a = To<int*>() + 100.0; // expected-error {{invalid operands to binary expression ('To<int *>' and 'double')}}
290 int *b = To<int*>() + To<double>(); // expected-error {{invalid operands to binary expression ('To<int *>' and 'To<double>')}}
291
292 #if __cplusplus > 201703L
293 enum E1 {};
294 enum E2 {};
295 auto c = To<E1>() <=> To<E2>(); // expected-error {{invalid operands to binary expression ('To<dr1687::E1>' and 'To<dr1687::E2>')}}
296 #endif
297 }
298
299 namespace dr1690 { // dr1690: 9
300 // See also the various tests in "CXX/basic/basic.lookup/basic.lookup.argdep".
301 #if __cplusplus >= 201103L
302 namespace N {
__anond5490a410102() 303 static auto lambda = []() { struct S {} s; return s; };
304 void f(decltype(lambda()));
305 }
306
test()307 void test() {
308 auto s = N::lambda();
309 f(s); // ok
310 }
311 #endif
312 }
313
314 namespace dr1691 { // dr1691: 9
315 #if __cplusplus >= 201103L
316 namespace N {
317 namespace M {
318 enum E : int;
319 void f(E);
320 }
321 enum M::E : int {};
322 void g(M::E); // expected-note {{declared here}}
323 }
test()324 void test() {
325 N::M::E e;
326 f(e); // ok
327 g(e); // expected-error {{use of undeclared}}
328 }
329 #endif
330 }
331
332 namespace dr1692 { // dr1692: 9
333 namespace N {
334 struct A {
335 struct B {
336 struct C {};
337 };
338 };
339 void f(A::B::C);
340 }
test()341 void test() {
342 N::A::B::C c;
343 f(c); // ok
344 }
345 }
346
347 namespace dr1696 { // dr1696: 7
348 namespace std_examples {
349 #if __cplusplus >= 201402L
350 extern struct A a;
351 struct A {
352 const A &x = { A{a, a} };
353 const A &y = { A{} }; // expected-error {{default member initializer for 'y' needed within definition of enclosing class 'A' outside of member functions}} expected-note {{here}}
354 };
355 A a{a, a};
356 #endif
357 }
358
359 struct A { A(); ~A(); };
360 #if __cplusplus >= 201103L
361 struct B {
362 A &&a; // expected-note {{declared here}}
Bdr1696::B363 B() : a{} {} // expected-error {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
364 } b;
365 #endif
366
367 struct C {
368 C();
369 const A &a; // expected-note {{declared here}}
370 };
C()371 C::C() : a(A()) {} // expected-error {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
372
373 #if __cplusplus >= 201103L
374 // This is OK in C++14 onwards, per DR1815, though we don't support that yet:
375 // D1 d1 = {};
376 // is equivalent to
377 // D1 d1 = {A()};
378 // ... which lifetime-extends the A temporary.
379 struct D1 {
380 #if __cplusplus < 201402L
381 // expected-error@-2 {{binds to a temporary}}
382 #endif
383 const A &a = A(); // expected-note {{default member init}}
384 };
385 D1 d1 = {};
386 #if __cplusplus < 201402L
387 // expected-note@-2 {{first required here}}
388 #else
389 // expected-warning-re@-4 {{sorry, lifetime extension {{.*}} not supported}}
390 #endif
391
392 struct D2 {
393 const A &a = A(); // expected-note {{default member init}}
D2dr1696::D2394 D2() {} // expected-error {{binds to a temporary}}
395 };
396
397 struct D3 { // expected-error {{binds to a temporary}}
398 const A &a = A(); // expected-note {{default member init}}
399 };
400 D3 d3; // expected-note {{first required here}}
401
402 struct haslist1 {
403 std::initializer_list<int> il; // expected-note {{'std::initializer_list' member}}
haslist1dr1696::haslist1404 haslist1(int i) : il{i, 2, 3} {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
405 };
406
407 struct haslist2 {
408 std::initializer_list<int> il; // expected-note {{'std::initializer_list' member}}
409 haslist2();
410 };
haslist2()411 haslist2::haslist2() : il{1, 2} {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
412
413 struct haslist3 {
414 std::initializer_list<int> il = {1, 2, 3};
415 };
416
417 struct haslist4 { // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
418 std::initializer_list<int> il = {1, 2, 3}; // expected-note {{default member initializer}}
419 };
420 haslist4 hl4; // expected-note {{in implicit default constructor}}
421
422 struct haslist5 {
423 std::initializer_list<int> il = {1, 2, 3}; // expected-note {{default member initializer}}
haslist5dr1696::haslist5424 haslist5() {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
425 };
426 #endif
427 }
428