summaryrefslogtreecommitdiffstats
path: root/test/SemaCXX/member-init.cpp
blob: b3ee30b456ec1f933c164cce1862e49e154d23af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s

struct Bitfield {
  int n : 3 = 7; // expected-error {{bitfield member cannot have an in-class initializer}}
};

int a;
class NoWarning {
  int &n = a;
public:
  int &GetN() { return n; }
};

bool b();
int k;
struct Recurse {
  int &n = // expected-error {{cannot use defaulted default constructor of 'Recurse' within the class outside of member functions because 'n' has an initializer}}
      b() ?
      Recurse().n : // expected-note {{implicit default constructor for 'Recurse' first required here}}
      k;
};

struct UnknownBound {
  int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
  int bs[4] = { 4, 5, 6, 7 };
  int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
};

template<int n> struct T { static const int B; };
template<> struct T<2> { template<int C, int D> using B = int; };
const int C = 0, D = 0;
struct S {
  int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
  T<sizeof(as) / sizeof(int)> x;
  // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
  operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
                      // expected-error {{array bound cannot be deduced from an in-class initializer}}
};

struct ThrowCtor { ThrowCtor(int) noexcept(false); };
struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };

struct Throw { ThrowCtor tc = 42; };
struct NoThrow { NoThrowCtor tc = 42; };

static_assert(!noexcept(Throw()), "incorrect exception specification");
static_assert(noexcept(NoThrow()), "incorrect exception specification");

struct CheckExcSpec {
  CheckExcSpec() noexcept(true) = default;
  int n = 0;
};
struct CheckExcSpecFail {
  CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
  ThrowCtor tc = 123;
};

struct TypedefInit {
  typedef int A = 0; // expected-error {{illegal initializer}}
};

// PR10578 / <rdar://problem/9877267>
namespace PR10578 {
  template<typename T>
  struct X { 
    X() {
      T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
    }
  };

  struct Y : X<int> {
    Y();
  };

  Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
  } catch(...) {
  }
}

namespace PR14838 {
  struct base { ~base() {} };
  class function : base {
    ~function() {} // expected-note {{implicitly declared private here}}
  public:
    function(...) {}
  };
  struct thing {};
  struct another {
    another() : r(thing()) {}
    // expected-error@-1 {{temporary of type 'const PR14838::function' has private destructor}}
    // expected-warning@-2 {{binding reference member 'r' to a temporary value}}
    const function &r; // expected-note {{reference member declared here}}
  } af;
}

namespace rdar14084171 {
  struct Point { // expected-note 3 {{candidate constructor}}
    double x;
    double y;
  };
  struct Sprite {
    Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
  };
  void f(Sprite& x) { x = x; }
}

namespace PR18560 {
  struct X { int m; };

  template<typename T = X,
           typename U = decltype(T::m)>
  int f();

  struct Y { int b = f(); };
}

namespace template_valid {
// Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
struct A {
  A();
  template <typename T>
  struct B { int m1 = sizeof(A) + sizeof(T); };
  B<int> m2;
};
A::A() {}
}

namespace template_default_ctor {
struct A {
  template <typename T>
  struct B {
    int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
  };
  // expected-note@+1 {{implicit default constructor for 'template_default_ctor::A::B<int>' first required here}}
  enum { NOE = noexcept(B<int>()) };
};
}

namespace default_ctor {
struct A {
  struct B {
    int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
  };
  // expected-note@+1 {{implicit default constructor for 'default_ctor::A::B' first required here}}
  enum { NOE = noexcept(B()) };
};
}

namespace member_template {
struct A {
  template <typename T>
  struct B {
    struct C {
      int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'C' within 'A' outside of member functions because 'm1' has an initializer}}
    };
    template <typename U>
    struct D {
      int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'D' within 'A' outside of member functions because 'm1' has an initializer}}
    };
  };
  enum {
    // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::C' first required here}}
    NOE1 = noexcept(B<int>::C()),
    // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::D<int>' first required here}}
    NOE2 = noexcept(B<int>::D<int>())
  };
};
}

namespace explicit_instantiation {
template<typename T> struct X {
  X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
  int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
};
template struct X<int>; // ok
template<typename T> X<T>::X() {}
template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
}

namespace local_class {
template<typename T> void f() {
  struct X { // expected-note {{in instantiation of default member initializer 'local_class::f()::X::n' requested here}}
    int n = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
  };
}
void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
}

namespace PR22056 {
template <int N>
struct S {
  int x[3] = {[N] = 3};
};
}
OpenPOWER on IntegriCloud