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}}
10   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 0-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 
71 void f() {
72   A as1[1] = { };
73   A as2[1] = { 1 };
74 #if __cplusplus <= 201402L
75   // expected-error@-2 {{copying array element of type 'A' invokes deleted constructor}}
76 #endif
77 
78   B b1 = { };
79   B b2 = { 1 };
80 #if __cplusplus <= 201402L
81   // expected-error@-2 {{copying member subobject of type 'A' invokes deleted constructor}}
82 #endif
83 
84   C c1 = { 1 };
85 }
86 
87 class Agg {
88 public:
89   int i, j;
90 };
91 
92 class AggAgg {
93 public:
94   Agg agg1;
95   Agg agg2;
96 };
97 
98 AggAgg aggagg = { 1, 2, 3, 4 };
99 
100 namespace diff_cpp14_dcl_init_aggr_example {
101   struct derived;
102   struct base {
103     friend struct derived;
104   private:
105     base();
106   };
107   struct derived : base {};
108 
109   derived d1{};
110 #if __cplusplus > 201402L
111   // expected-error@-2 {{private}}
112   // expected-note@-7 {{here}}
113 #endif
114   derived d2;
115 }
116 
117 namespace ProtectedBaseCtor {
118   // FIXME: It's unclear whether f() and g() should be valid in C++1z. What is
119   // the object expression in a constructor call -- the base class subobject or
120   // the complete object?
121   struct A {
122   protected:
123     A();
124   };
125 
126   struct B : public A {
127     friend B f();
128     friend B g();
129     friend B h();
130   };
131 
132   B f() { return {}; }
133 #if __cplusplus > 201402L
134   // expected-error@-2 {{protected default constructor}}
135   // expected-note@-12 {{here}}
136 #endif
137 
138   B g() { return {{}}; }
139 #if __cplusplus <= 201402L
140   // expected-error@-2 {{no matching constructor}}
141   // expected-note@-15 3{{candidate}}
142 #else
143   // expected-error@-5 {{protected default constructor}}
144   // expected-note@-21 {{here}}
145 #endif
146 
147   B h() { return {A{}}; }
148 #if __cplusplus <= 201402L
149   // expected-error@-2 {{no matching constructor}}
150   // expected-note@-24 3{{candidate}}
151 #endif
152   // expected-error@-5 {{protected constructor}}
153   // expected-note@-30 {{here}}
154 }
155