Clang Project

clang_source_code/test/SemaCXX/default-constructor-initializers.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
5struct X1 { // has no implicit default constructor
6   X1(int);
7};
8
9struct X2  : X1 {
10#if __cplusplus <= 199711L
11// expected-note@-2 2 {{'X2' declared here}}
12#endif
13
14   X2(int);
15};
16
17struct X3 : public X2 {
18#if __cplusplus <= 199711L
19// expected-error@-2 {{implicit default constructor for 'X3' must explicitly initialize the base class 'X2' which does not have a default constructor}}
20#else
21// expected-note@-4 {{default constructor of 'X3' is implicitly deleted because base class 'X2' has no default constructor}}
22#endif
23};
24
25X3 x3;
26#if __cplusplus <= 199711L
27// expected-note@-2 {{first required here}}
28#else
29// expected-error@-4 {{call to implicitly-deleted default constructor of 'X3'}}
30#endif
31
32struct X4 {
33#if __cplusplus <= 199711L
34// expected-error@-2 {{must explicitly initialize the member 'x2'}}
35// expected-error@-3 {{must explicitly initialize the reference member 'rx2'}}
36#endif
37
38  X2 x2;
39#if __cplusplus <= 199711L
40  // expected-note@-2 {{member is declared here}}
41#else
42  // expected-note@-4 {{default constructor of 'X4' is implicitly deleted because field 'x2' has no default constructor}}
43#endif
44
45  X2 & rx2;
46#if __cplusplus <= 199711L
47  // expected-note@-2 {{declared here}}
48#endif
49};
50
51X4 x4;
52#if __cplusplus <= 199711L
53// expected-note@-2 {{first required here}}
54#else
55// expected-error@-4 {{call to implicitly-deleted default constructor of 'X4'}}
56#endif
57
58struct Y1 { // has no implicit default constructor
59   Y1(int);
60};
61
62struct Y2  : Y1 { 
63   Y2(int);
64   Y2();
65};
66
67struct Y3 : public Y2 {
68};
69Y3 y3; 
70
71struct Y4 {
72  Y2 y2; 
73};
74
75Y4 y4;
76
77// More tests
78
79struct Z1 {
80#if __cplusplus <= 199711L
81// expected-error@-2 {{must explicitly initialize the reference member 'z'}}
82// expected-error@-3 {{must explicitly initialize the const member 'c1'}}
83#endif
84
85  int& z;
86#if __cplusplus <= 199711L
87  // expected-note@-2 {{declared here}}
88#else
89  // expected-note@-4 {{default constructor of 'Z1' is implicitly deleted because field 'z' of reference type 'int &' would not be initialized}}
90#endif
91
92  const int c1;
93#if __cplusplus <= 199711L
94  // expected-note@-2 {{declared here}}
95#endif
96  volatile int v1;
97};
98
99// Test default initialization which *requires* a constructor call for non-POD.
100Z1 z1;
101#if __cplusplus <= 199711L
102// expected-note@-2 {{first required here}}
103#else
104// expected-error@-4 {{call to implicitly-deleted default constructor of 'Z1'}}
105#endif
106
107// Ensure that value initialization doesn't use trivial implicit constructors.
108namespace PR7948 {
109  // Note that this is also non-POD to ensure we don't just special case PODs.
110  struct S { const int x; ~S(); };
111  const S arr[2] = { { 42 } };
112}
113
114// This is valid
115union U {
116  const int i;
117  float f;
118};
119U u;
120