xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/member-init.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc struct Bitfield {
4f4a2713aSLionel Sambuc   int n : 3 = 7; // expected-error {{bitfield member cannot have an in-class initializer}}
5f4a2713aSLionel Sambuc };
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc int a;
8f4a2713aSLionel Sambuc class NoWarning {
9f4a2713aSLionel Sambuc   int &n = a;
10f4a2713aSLionel Sambuc public:
GetN()11f4a2713aSLionel Sambuc   int &GetN() { return n; }
12f4a2713aSLionel Sambuc };
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc bool b();
15f4a2713aSLionel Sambuc int k;
16f4a2713aSLionel Sambuc struct Recurse {
17*0a6a1f1dSLionel Sambuc   int &n = // expected-error {{cannot use defaulted default constructor of 'Recurse' within the class outside of member functions because 'n' has an initializer}}
18*0a6a1f1dSLionel Sambuc       b() ?
19*0a6a1f1dSLionel Sambuc       Recurse().n : // expected-note {{implicit default constructor for 'Recurse' first required here}}
20*0a6a1f1dSLionel Sambuc       k;
21f4a2713aSLionel Sambuc };
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc struct UnknownBound {
24f4a2713aSLionel Sambuc   int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
25f4a2713aSLionel Sambuc   int bs[4] = { 4, 5, 6, 7 };
26f4a2713aSLionel Sambuc   int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
27f4a2713aSLionel Sambuc };
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc template<int n> struct T { static const int B; };
30f4a2713aSLionel Sambuc template<> struct T<2> { template<int C, int D> using B = int; };
31f4a2713aSLionel Sambuc const int C = 0, D = 0;
32f4a2713aSLionel Sambuc struct S {
33f4a2713aSLionel Sambuc   int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
34f4a2713aSLionel Sambuc   T<sizeof(as) / sizeof(int)> x;
35f4a2713aSLionel Sambuc   // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
operator int[]S36f4a2713aSLionel Sambuc   operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
37f4a2713aSLionel Sambuc                       // expected-error {{array bound cannot be deduced from an in-class initializer}}
38f4a2713aSLionel Sambuc };
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc struct ThrowCtor { ThrowCtor(int) noexcept(false); };
41f4a2713aSLionel Sambuc struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc struct Throw { ThrowCtor tc = 42; };
44f4a2713aSLionel Sambuc struct NoThrow { NoThrowCtor tc = 42; };
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc static_assert(!noexcept(Throw()), "incorrect exception specification");
47f4a2713aSLionel Sambuc static_assert(noexcept(NoThrow()), "incorrect exception specification");
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc struct CheckExcSpec {
50f4a2713aSLionel Sambuc   CheckExcSpec() noexcept(true) = default;
51f4a2713aSLionel Sambuc   int n = 0;
52f4a2713aSLionel Sambuc };
53f4a2713aSLionel Sambuc struct CheckExcSpecFail {
54f4a2713aSLionel Sambuc   CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
55f4a2713aSLionel Sambuc   ThrowCtor tc = 123;
56f4a2713aSLionel Sambuc };
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc struct TypedefInit {
59f4a2713aSLionel Sambuc   typedef int A = 0; // expected-error {{illegal initializer}}
60f4a2713aSLionel Sambuc };
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc // PR10578 / <rdar://problem/9877267>
63f4a2713aSLionel Sambuc namespace PR10578 {
64f4a2713aSLionel Sambuc   template<typename T>
65f4a2713aSLionel Sambuc   struct X {
XPR10578::X66f4a2713aSLionel Sambuc     X() {
67f4a2713aSLionel Sambuc       T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
68f4a2713aSLionel Sambuc     }
69f4a2713aSLionel Sambuc   };
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc   struct Y : X<int> {
72f4a2713aSLionel Sambuc     Y();
73f4a2713aSLionel Sambuc   };
74f4a2713aSLionel Sambuc 
Y()75f4a2713aSLionel Sambuc   Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
76f4a2713aSLionel Sambuc   } catch(...) {
77f4a2713aSLionel Sambuc   }
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc namespace PR14838 {
~basePR14838::base81f4a2713aSLionel Sambuc   struct base { ~base() {} };
82f4a2713aSLionel Sambuc   class function : base {
~function()83f4a2713aSLionel Sambuc     ~function() {} // expected-note {{implicitly declared private here}}
84f4a2713aSLionel Sambuc   public:
function(...)85f4a2713aSLionel Sambuc     function(...) {}
86f4a2713aSLionel Sambuc   };
87f4a2713aSLionel Sambuc   struct thing {};
88f4a2713aSLionel Sambuc   struct another {
anotherPR14838::another89f4a2713aSLionel Sambuc     another() : r(thing()) {}
90f4a2713aSLionel Sambuc     // expected-error@-1 {{temporary of type 'const PR14838::function' has private destructor}}
91f4a2713aSLionel Sambuc     // expected-warning@-2 {{binding reference member 'r' to a temporary value}}
92f4a2713aSLionel Sambuc     const function &r; // expected-note {{reference member declared here}}
93f4a2713aSLionel Sambuc   } af;
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc namespace rdar14084171 {
97f4a2713aSLionel Sambuc   struct Point { // expected-note 3 {{candidate constructor}}
98f4a2713aSLionel Sambuc     double x;
99f4a2713aSLionel Sambuc     double y;
100f4a2713aSLionel Sambuc   };
101f4a2713aSLionel Sambuc   struct Sprite {
102f4a2713aSLionel Sambuc     Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
103f4a2713aSLionel Sambuc   };
f(Sprite & x)104f4a2713aSLionel Sambuc   void f(Sprite& x) { x = x; }
105f4a2713aSLionel Sambuc }
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc namespace PR18560 {
108*0a6a1f1dSLionel Sambuc   struct X { int m; };
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc   template<typename T = X,
111*0a6a1f1dSLionel Sambuc            typename U = decltype(T::m)>
112*0a6a1f1dSLionel Sambuc   int f();
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc   struct Y { int b = f(); };
115*0a6a1f1dSLionel Sambuc }
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc namespace template_valid {
118*0a6a1f1dSLionel Sambuc // Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
119*0a6a1f1dSLionel Sambuc struct A {
120*0a6a1f1dSLionel Sambuc   A();
121*0a6a1f1dSLionel Sambuc   template <typename T>
122*0a6a1f1dSLionel Sambuc   struct B { int m1 = sizeof(A) + sizeof(T); };
123*0a6a1f1dSLionel Sambuc   B<int> m2;
124*0a6a1f1dSLionel Sambuc };
A()125*0a6a1f1dSLionel Sambuc A::A() {}
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc namespace template_default_ctor {
129*0a6a1f1dSLionel Sambuc struct A {
130*0a6a1f1dSLionel Sambuc   template <typename T>
131*0a6a1f1dSLionel Sambuc   struct B {
132*0a6a1f1dSLionel Sambuc     int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
133*0a6a1f1dSLionel Sambuc   };
134*0a6a1f1dSLionel Sambuc   // expected-note@+1 {{implicit default constructor for 'template_default_ctor::A::B<int>' first required here}}
135*0a6a1f1dSLionel Sambuc   enum { NOE = noexcept(B<int>()) };
136*0a6a1f1dSLionel Sambuc };
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc namespace default_ctor {
140*0a6a1f1dSLionel Sambuc struct A {
141*0a6a1f1dSLionel Sambuc   struct B {
142*0a6a1f1dSLionel Sambuc     int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
143*0a6a1f1dSLionel Sambuc   };
144*0a6a1f1dSLionel Sambuc   // expected-note@+1 {{implicit default constructor for 'default_ctor::A::B' first required here}}
145*0a6a1f1dSLionel Sambuc   enum { NOE = noexcept(B()) };
146*0a6a1f1dSLionel Sambuc };
147*0a6a1f1dSLionel Sambuc }
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc namespace member_template {
150*0a6a1f1dSLionel Sambuc struct A {
151*0a6a1f1dSLionel Sambuc   template <typename T>
152*0a6a1f1dSLionel Sambuc   struct B {
153*0a6a1f1dSLionel Sambuc     struct C {
154*0a6a1f1dSLionel Sambuc       int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'C' within 'A' outside of member functions because 'm1' has an initializer}}
155*0a6a1f1dSLionel Sambuc     };
156*0a6a1f1dSLionel Sambuc     template <typename U>
157*0a6a1f1dSLionel Sambuc     struct D {
158*0a6a1f1dSLionel Sambuc       int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'D' within 'A' outside of member functions because 'm1' has an initializer}}
159*0a6a1f1dSLionel Sambuc     };
160*0a6a1f1dSLionel Sambuc   };
161*0a6a1f1dSLionel Sambuc   enum {
162*0a6a1f1dSLionel Sambuc     // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::C' first required here}}
163*0a6a1f1dSLionel Sambuc     NOE1 = noexcept(B<int>::C()),
164*0a6a1f1dSLionel Sambuc     // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::D<int>' first required here}}
165*0a6a1f1dSLionel Sambuc     NOE2 = noexcept(B<int>::D<int>())
166*0a6a1f1dSLionel Sambuc   };
167*0a6a1f1dSLionel Sambuc };
168*0a6a1f1dSLionel Sambuc }
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc namespace explicit_instantiation {
171*0a6a1f1dSLionel Sambuc template<typename T> struct X {
172*0a6a1f1dSLionel Sambuc   X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
173*0a6a1f1dSLionel Sambuc   int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
174*0a6a1f1dSLionel Sambuc };
175*0a6a1f1dSLionel Sambuc template struct X<int>; // ok
X()176*0a6a1f1dSLionel Sambuc template<typename T> X<T>::X() {}
177*0a6a1f1dSLionel Sambuc template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc namespace local_class {
f()181*0a6a1f1dSLionel Sambuc template<typename T> void f() {
182*0a6a1f1dSLionel Sambuc   struct X { // expected-note {{in instantiation of default member initializer 'local_class::f()::X::n' requested here}}
183*0a6a1f1dSLionel Sambuc     int n = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
184*0a6a1f1dSLionel Sambuc   };
185*0a6a1f1dSLionel Sambuc }
g()186*0a6a1f1dSLionel Sambuc void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
187*0a6a1f1dSLionel Sambuc }
188*0a6a1f1dSLionel Sambuc 
189*0a6a1f1dSLionel Sambuc namespace PR22056 {
190*0a6a1f1dSLionel Sambuc template <int N>
191*0a6a1f1dSLionel Sambuc struct S {
192*0a6a1f1dSLionel Sambuc   int x[3] = {[N] = 3};
193*0a6a1f1dSLionel Sambuc };
194*0a6a1f1dSLionel Sambuc }
195