xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/member-init.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct Bitfield {
4*f4a2713aSLionel Sambuc   int n : 3 = 7; // expected-error {{bitfield member cannot have an in-class initializer}}
5*f4a2713aSLionel Sambuc };
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc int a;
8*f4a2713aSLionel Sambuc class NoWarning {
9*f4a2713aSLionel Sambuc   int &n = a;
10*f4a2713aSLionel Sambuc public:
11*f4a2713aSLionel Sambuc   int &GetN() { return n; }
12*f4a2713aSLionel Sambuc };
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc bool b();
15*f4a2713aSLionel Sambuc int k;
16*f4a2713aSLionel Sambuc struct Recurse {
17*f4a2713aSLionel Sambuc   int &n = b() ? Recurse().n : k; // expected-error {{defaulted default constructor of 'Recurse' cannot be used by non-static data member initializer which appears before end of class definition}}
18*f4a2713aSLionel Sambuc };
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc struct UnknownBound {
21*f4a2713aSLionel Sambuc   int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
22*f4a2713aSLionel Sambuc   int bs[4] = { 4, 5, 6, 7 };
23*f4a2713aSLionel Sambuc   int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
24*f4a2713aSLionel Sambuc };
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc template<int n> struct T { static const int B; };
27*f4a2713aSLionel Sambuc template<> struct T<2> { template<int C, int D> using B = int; };
28*f4a2713aSLionel Sambuc const int C = 0, D = 0;
29*f4a2713aSLionel Sambuc struct S {
30*f4a2713aSLionel Sambuc   int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
31*f4a2713aSLionel Sambuc   T<sizeof(as) / sizeof(int)> x;
32*f4a2713aSLionel Sambuc   // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
33*f4a2713aSLionel Sambuc   operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
34*f4a2713aSLionel Sambuc                       // expected-error {{array bound cannot be deduced from an in-class initializer}}
35*f4a2713aSLionel Sambuc };
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc struct ThrowCtor { ThrowCtor(int) noexcept(false); };
38*f4a2713aSLionel Sambuc struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc struct Throw { ThrowCtor tc = 42; };
41*f4a2713aSLionel Sambuc struct NoThrow { NoThrowCtor tc = 42; };
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc static_assert(!noexcept(Throw()), "incorrect exception specification");
44*f4a2713aSLionel Sambuc static_assert(noexcept(NoThrow()), "incorrect exception specification");
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc struct CheckExcSpec {
47*f4a2713aSLionel Sambuc   CheckExcSpec() noexcept(true) = default;
48*f4a2713aSLionel Sambuc   int n = 0;
49*f4a2713aSLionel Sambuc };
50*f4a2713aSLionel Sambuc struct CheckExcSpecFail {
51*f4a2713aSLionel Sambuc   CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
52*f4a2713aSLionel Sambuc   ThrowCtor tc = 123;
53*f4a2713aSLionel Sambuc };
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc struct TypedefInit {
56*f4a2713aSLionel Sambuc   typedef int A = 0; // expected-error {{illegal initializer}}
57*f4a2713aSLionel Sambuc };
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc // PR10578 / <rdar://problem/9877267>
60*f4a2713aSLionel Sambuc namespace PR10578 {
61*f4a2713aSLionel Sambuc   template<typename T>
62*f4a2713aSLionel Sambuc   struct X {
63*f4a2713aSLionel Sambuc     X() {
64*f4a2713aSLionel Sambuc       T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
65*f4a2713aSLionel Sambuc     }
66*f4a2713aSLionel Sambuc   };
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc   struct Y : X<int> {
69*f4a2713aSLionel Sambuc     Y();
70*f4a2713aSLionel Sambuc   };
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc   Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
73*f4a2713aSLionel Sambuc   } catch(...) {
74*f4a2713aSLionel Sambuc   }
75*f4a2713aSLionel Sambuc }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc namespace PR14838 {
78*f4a2713aSLionel Sambuc   struct base { ~base() {} };
79*f4a2713aSLionel Sambuc   class function : base {
80*f4a2713aSLionel Sambuc     ~function() {} // expected-note {{implicitly declared private here}}
81*f4a2713aSLionel Sambuc   public:
82*f4a2713aSLionel Sambuc     function(...) {}
83*f4a2713aSLionel Sambuc   };
84*f4a2713aSLionel Sambuc   struct thing {};
85*f4a2713aSLionel Sambuc   struct another {
86*f4a2713aSLionel Sambuc     another() : r(thing()) {}
87*f4a2713aSLionel Sambuc     // expected-error@-1 {{temporary of type 'const PR14838::function' has private destructor}}
88*f4a2713aSLionel Sambuc     // expected-warning@-2 {{binding reference member 'r' to a temporary value}}
89*f4a2713aSLionel Sambuc     const function &r; // expected-note {{reference member declared here}}
90*f4a2713aSLionel Sambuc   } af;
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc namespace rdar14084171 {
94*f4a2713aSLionel Sambuc   struct Point { // expected-note 3 {{candidate constructor}}
95*f4a2713aSLionel Sambuc     double x;
96*f4a2713aSLionel Sambuc     double y;
97*f4a2713aSLionel Sambuc   };
98*f4a2713aSLionel Sambuc   struct Sprite {
99*f4a2713aSLionel Sambuc     Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
100*f4a2713aSLionel Sambuc   };
101*f4a2713aSLionel Sambuc   void f(Sprite& x) { x = x; }
102*f4a2713aSLionel Sambuc }
103