1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
3 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
4 | |
5 | // A program that calls for default-initialization or value-initialization of |
6 | // an entity of reference type is illformed. If T is a cv-qualified type, the |
7 | // cv-unqualified version of T is used for these definitions of |
8 | // zero-initialization, default-initialization, and value-initialization. |
9 | |
10 | typedef int &IR; |
11 | IR r; // expected-error {{declaration of reference variable 'r' requires an initializer}} |
12 | int n = IR(); // expected-error {{reference to type 'int' requires an initializer}} |
13 | |
14 | #if __cplusplus < 201103L |
15 | struct S { // expected-error {{implicit default constructor for 'S' must explicitly initialize the reference member}} |
16 | int &x; // expected-note {{declared here}} expected-error 3{{reference to type 'int' requires an initializer}} |
17 | }; |
18 | S s; // expected-note {{implicit default constructor for 'S' first required here}} |
19 | S f() { |
20 | return S(); // expected-note {{in value-initialization of type 'S' here}} |
21 | } |
22 | |
23 | struct T |
24 | : S { // expected-note 2{{in value-initialization of type 'S' here}} |
25 | }; |
26 | T t = T(); // expected-note {{in value-initialization of type 'T' here}} |
27 | |
28 | struct U { |
29 | T t[3]; // expected-note {{in value-initialization of type 'T' here}} |
30 | }; |
31 | U u = U(); // expected-note {{in value-initialization of type 'U' here}} |
32 | #else |
33 | struct S { |
34 | int &x; // expected-note 4{{because field 'x' of reference type 'int &' would not be initialized}} |
35 | }; |
36 | S s; // expected-error {{deleted default constructor}} |
37 | S f() { |
38 | return S(); // expected-error {{deleted default constructor}} |
39 | } |
40 | |
41 | struct T |
42 | : S { // expected-note 2{{because base class 'S' has a deleted default constructor}} |
43 | }; |
44 | T t = T(); // expected-error {{deleted default constructor}} |
45 | |
46 | struct U { |
47 | T t[3]; // expected-note {{because field 't' has a deleted default constructor}} |
48 | }; |
49 | U u = U(); // expected-error {{deleted default constructor}} |
50 | #endif |
51 | |
52 | // Ensure that we handle C++11 in-class initializers properly as an extension. |
53 | // In this case, there is no user-declared default constructor, so we |
54 | // recursively apply the value-initialization checks, but we will emit a |
55 | // constructor call anyway, because the default constructor is not trivial. |
56 | struct V { |
57 | int n; |
58 | int &r = n; // expected-warning 0-1{{C++11}} |
59 | }; |
60 | V v = V(); // ok |
61 | struct W { |
62 | int n; |
63 | S s = { n }; // expected-warning 0-1{{C++11}} |
64 | }; |
65 | W w = W(); // ok |
66 | |
67 | // Ensure we're not faking this up by making the default constructor |
68 | // non-trivial. |
69 | _Static_assert(__has_trivial_constructor(S), ""); |
70 | _Static_assert(__has_trivial_constructor(T), ""); |
71 | _Static_assert(__has_trivial_constructor(U), ""); |
72 | _Static_assert(!__has_trivial_constructor(V), ""); |
73 | _Static_assert(!__has_trivial_constructor(W), ""); |
74 | |