1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z %s
4 
5 // Verify that using an initializer list for a non-aggregate looks for
6 // constructors..
7 // Note that due to a (likely) standard bug, this is technically an aggregate,
8 // but we do not treat it as one.
9 struct NonAggr1 { // expected-note 2 {{candidate constructor}}
NonAggr1NonAggr110   NonAggr1(int, int) { } // expected-note {{candidate constructor}}
11 
12   int m;
13 };
14 
15 struct Base { };
16 struct NonAggr2 : public Base { // expected-note 0-3 {{candidate constructor}}
17   int m;
18 };
19 
20 class NonAggr3 { // expected-note 3 {{candidate constructor}}
21   int m;
22 };
23 
24 struct NonAggr4 { // expected-note 3 {{candidate constructor}}
25   int m;
26   virtual void f();
27 };
28 
29 NonAggr1 na1 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr1'}}
30 NonAggr2 na2 = { 17 };
31 NonAggr3 na3 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr3'}}
32 NonAggr4 na4 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr4'}}
33 #if __cplusplus <= 201402L
34 // expected-error@-4{{no matching constructor for initialization of 'NonAggr2'}}
35 #else
36 // expected-error@-6{{requires explicit braces}}
37 NonAggr2 na2b = { {}, 17 }; // ok
38 #endif
39 
40 // PR5817
41 typedef int type[][2];
42 const type foo = {0};
43 
44 // Vector initialization.
45 typedef short __v4hi __attribute__ ((__vector_size__ (8)));
46 __v4hi v1 = { (void *)1, 2, 3 }; // expected-error {{cannot initialize a vector element of type 'short' with an rvalue of type 'void *'}}
47 
48 // Array initialization.
49 int a[] = { (void *)1 }; // expected-error {{cannot initialize an array element of type 'int' with an rvalue of type 'void *'}}
50 
51 // Struct initialization.
52 struct S { int a; } s = { (void *)1 }; // expected-error {{cannot initialize a member subobject of type 'int' with an rvalue of type 'void *'}}
53 
54 // Check that we're copy-initializing the structs.
55 struct A {
56   A();
57   A(int);
58   ~A();
59 
60   A(const A&) = delete; // expected-note 2 {{'A' has been explicitly marked deleted here}}
61 };
62 
63 struct B {
64   A a;
65 };
66 
67 struct C {
68   const A& a;
69 };
70 
f()71 void f() {
72   A as1[1] = { };
73   A as2[1] = { 1 }; // expected-error {{copying array element of type 'A' invokes deleted constructor}}
74 
75   B b1 = { };
76   B b2 = { 1 }; // expected-error {{copying member subobject of type 'A' invokes deleted constructor}}
77 
78   C c1 = { 1 };
79 }
80 
81 class Agg {
82 public:
83   int i, j;
84 };
85 
86 class AggAgg {
87 public:
88   Agg agg1;
89   Agg agg2;
90 };
91 
92 AggAgg aggagg = { 1, 2, 3, 4 };
93 
94 namespace diff_cpp14_dcl_init_aggr_example {
95   struct derived;
96   struct base {
97     friend struct derived;
98   private:
99     base();
100   };
101   struct derived : base {};
102 
103   derived d1{};
104 #if __cplusplus > 201402L
105   // expected-error@-2 {{private}}
106   // expected-note@-7 {{here}}
107 #endif
108   derived d2;
109 }
110 
111 namespace ProtectedBaseCtor {
112   // FIXME: It's unclear whether f() and g() should be valid in C++1z. What is
113   // the object expression in a constructor call -- the base class subobject or
114   // the complete object?
115   struct A {
116   protected:
117     A();
118   };
119 
120   struct B : public A {
121     friend B f();
122     friend B g();
123     friend B h();
124   };
125 
f()126   B f() { return {}; }
127 #if __cplusplus > 201402L
128   // expected-error@-2 {{protected default constructor}}
129   // expected-note@-12 {{here}}
130 #endif
131 
g()132   B g() { return {{}}; }
133 #if __cplusplus <= 201402L
134   // expected-error@-2 {{no matching constructor}}
135   // expected-note@-15 3{{candidate}}
136 #else
137   // expected-error@-5 {{protected default constructor}}
138   // expected-note@-21 {{here}}
139 #endif
140 
h()141   B h() { return {A{}}; }
142 #if __cplusplus <= 201402L
143   // expected-error@-2 {{no matching constructor}}
144   // expected-note@-24 3{{candidate}}
145 #endif
146   // expected-error@-5 {{protected constructor}}
147   // expected-note@-30 {{here}}
148 }
149