Clang Project

clang_source_code/test/SemaCXX/member-init.cpp
1// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
2
3struct Bitfield {
4  int n : 3 = 7; // expected-warning {{C++2a extension}} expected-warning {{changes value from 7 to -1}}
5};
6
7int a;
8class NoWarning {
9  int &n = a;
10public:
11  int &GetN() { return n; }
12};
13
14bool b();
15int k;
16struct 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}}
20      k;
21};
22
23struct UnknownBound {
24  int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
25  int bs[4] = { 4, 5, 6, 7 };
26  int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
27};
28
29template<int n> struct T { static const int B; };
30template<> struct T<2> { template<int C, int D> using B = int; };
31const int C = 0, D = 0;
32struct S {
33  int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class 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 an in-class initializer}}
38};
39
40struct ThrowCtor { ThrowCtor(int) noexcept(false); };
41struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
42
43struct Throw { ThrowCtor tc = 42; };
44struct NoThrow { NoThrowCtor tc = 42; };
45
46static_assert(!noexcept(Throw()), "incorrect exception specification");
47static_assert(noexcept(NoThrow()), "incorrect exception specification");
48
49struct CheckExcSpec {
50  CheckExcSpec() noexcept(true) = default;
51  int n = 0;
52};
53struct CheckExcSpecFail {
54  CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
55  ThrowCtor tc = 123;
56};
57
58struct TypedefInit {
59  typedef int A = 0; // expected-error {{illegal initializer}}
60};
61
62// PR10578 / <rdar://problem/9877267>
63namespace 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'}}
68    }
69  };
70
71  struct Y : X<int> {
72    Y();
73  };
74
75  Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
76  } catch(...) {
77  }
78}
79
80namespace PR14838 {
81  struct base { ~base() {} };
82  class function : base {
83    ~function() {} // expected-note {{implicitly declared private here}}
84  public:
85    function(...) {}
86  };
87  struct thing {};
88  struct another {
89    another() : r(thing()) {} // expected-error {{binds to a temporary object}}
90    // expected-error@-1 {{temporary of type 'PR14838::function' has private destructor}}
91    const function &r; // expected-note {{reference member declared here}}
92  } af;
93}
94
95namespace rdar14084171 {
96  struct Point { // expected-note 3 {{candidate constructor}}
97    double x;
98    double y;
99  };
100  struct Sprite {
101    Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
102  };
103  void f(Sprite& x) { x = x; } // expected-warning {{explicitly assigning value of variable}}
104}
105
106namespace PR18560 {
107  struct X { int m; };
108
109  template<typename T = X,
110           typename U = decltype(T::m)>
111  int f();
112
113  struct Y { int b = f(); };
114}
115
116namespace template_valid {
117// Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
118struct A {
119  A();
120  template <typename T>
121  struct B { int m1 = sizeof(A) + sizeof(T); };
122  B<int> m2;
123};
124A::A() {}
125}
126
127namespace template_default_ctor {
128struct A {
129  template <typename T>
130  struct B { // expected-error {{initializer for 'm1' needed}}
131    int m1 = 0; // expected-note {{declared here}}
132  };
133  enum { NOE = noexcept(B<int>()) }; // expected-note {{in evaluation of exception spec}}
134};
135}
136
137namespace default_ctor {
138struct A {
139  struct B { // expected-error {{initializer for 'm1' needed}}
140    int m1 = 0; // expected-note {{declared here}}
141  };
142  enum { NOE = noexcept(B()) }; // expected-note {{in evaluation of exception spec}}
143};
144}
145
146namespace member_template {
147struct 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}}
152    };
153    template <typename U>
154    struct D { // expected-error {{initializer for 'm1' needed}}
155      int m1 = 0; // expected-note {{declared here}}
156    };
157  };
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}}
161  };
162};
163}
164
165namespace explicit_instantiation {
166template<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}}
169};
170template struct X<int>; // ok
171template<typename T> X<T>::X() {}
172template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
173}
174
175namespace local_class {
176template<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}}
179  };
180}
181void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
182}
183
184namespace PR22056 {
185template <int N>
186struct S {
187  int x[3] = {[N] = 3};
188};
189}
190
191namespace PR28060 {
192template <class T>
193void foo(T v) {
194  struct s {
195    T *s = 0;
196  };
197}
198template void foo(int);
199}
200