[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / member-init.cpp
blob4dd3bd2ca94f7ce453453f6083bf8c76892ef177
1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
3 struct Bitfield {
4 int n : 3 = 7; // expected-warning {{C++20 extension}} expected-warning {{changes value from 7 to -1}}
5 };
7 int a;
8 class NoWarning {
9 int &n = a;
10 public:
11 int &GetN() { return n; }
14 bool b();
15 int k;
16 struct Recurse { // expected-error {{initializer for 'n' needed}}
17 int &n = // expected-note {{declared here}}
18 b() ?
19 Recurse().n : // expected-note {{in evaluation of exception spec}}
23 struct UnknownBound {
24 int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from a default member initializer}}
25 int bs[4] = { 4, 5, 6, 7 };
26 int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from a default member initializer}}
29 template<int n> struct T { static const int B; };
30 template<> struct T<2> { template<int C, int D> using B = int; };
31 const int C = 0, D = 0;
32 struct S {
33 int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from a default member initializer}}
34 T<sizeof(as) / sizeof(int)> x;
35 // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
36 operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
37 // expected-error {{array bound cannot be deduced from a default member initializer}}
40 struct ThrowCtor { ThrowCtor(int) noexcept(false); };
41 struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
43 struct Throw { ThrowCtor tc = 42; };
44 struct NoThrow { NoThrowCtor tc = 42; };
46 static_assert(!noexcept(Throw()), "incorrect exception specification");
47 static_assert(noexcept(NoThrow()), "incorrect exception specification");
49 struct CheckExcSpec {
50 CheckExcSpec() noexcept(true) = default;
51 int n = 0;
53 struct CheckExcSpecFail {
54 CheckExcSpecFail() noexcept(true) = default; // ok, but calls terminate() on exception
55 ThrowCtor tc = 123;
58 struct TypedefInit {
59 typedef int A = 0; // expected-error {{illegal initializer}}
62 // PR10578 / <rdar://problem/9877267>
63 namespace PR10578 {
64 template<typename T>
65 struct X {
66 X() {
67 T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} expected-warning {{unused variable}}
71 struct Y : X<int> {
72 Y();
75 Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
76 } catch(...) {
80 namespace PR14838 {
81 struct base { ~base() {} };
82 class function : base {
83 ~function() {} // expected-note {{implicitly declared private here}}
84 public:
85 function(...) {}
87 struct thing {};
88 struct another {
89 another() : r(thing()) {} // expected-error {{binds to a temporary object}}
90 // expected-error@-1 {{temporary of type 'function' has private destructor}}
91 const function &r; // expected-note {{reference member declared here}}
92 } af;
95 namespace rdar14084171 {
96 struct Point { // expected-note 3 {{candidate constructor}}
97 double x;
98 double y;
100 struct Sprite {
101 Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'Point'}}
103 void f(Sprite& x) { x = x; } // expected-warning {{explicitly assigning value of variable}}
106 namespace PR18560 {
107 struct X { int m; };
109 template<typename T = X,
110 typename U = decltype(T::m)>
111 int f();
113 struct Y { int b = f(); };
116 namespace template_valid {
117 // Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
118 struct A {
119 A();
120 template <typename T>
121 struct B { int m1 = sizeof(A) + sizeof(T); };
122 B<int> m2;
124 A::A() {}
127 namespace template_default_ctor {
128 struct A {
129 template <typename T>
130 struct B { // expected-error {{initializer for 'm1' needed}}
131 int m1 = 0; // expected-note {{declared here}}
133 enum { NOE = noexcept(B<int>()) }; // expected-note {{in evaluation of exception spec}}
137 namespace default_ctor {
138 struct A {
139 struct B { // expected-error {{initializer for 'm1' needed}}
140 int m1 = 0; // expected-note {{declared here}}
142 enum { NOE = noexcept(B()) }; // expected-note {{in evaluation of exception spec}}
146 namespace member_template {
147 struct A {
148 template <typename T>
149 struct B {
150 struct C { // expected-error {{initializer for 'm1' needed}}
151 int m1 = 0; // expected-note {{declared here}}
153 template <typename U>
154 struct D { // expected-error {{initializer for 'm1' needed}}
155 int m1 = 0; // expected-note {{declared here}}
158 enum {
159 NOE1 = noexcept(B<int>::C()), // expected-note {{in evaluation of exception spec}}
160 NOE2 = noexcept(B<int>::D<int>()) // expected-note {{in evaluation of exception spec}}
165 namespace explicit_instantiation {
166 template<typename T> struct X {
167 X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
168 int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
170 template struct X<int>; // ok
171 template<typename T> X<T>::X() {}
172 template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
175 namespace local_class {
176 template<typename T> void f() {
177 struct X { // expected-note {{in instantiation of default member initializer 'local_class::f()::X::n' requested here}}
178 int n = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
181 void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
184 namespace PR22056 {
185 template <int N>
186 struct S {
187 int x[3] = {[N] = 3}; // expected-warning {{C99 extension}}
191 namespace PR28060 {
192 template <class T>
193 void foo(T v) {
194 struct s {
195 T *s = 0;
198 template void foo(int);