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++17 %s
4 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
6 // Verify that using an initializer list for a non-aggregate looks for
8 struct NonAggr1
{ // expected-note 2 {{candidate constructor}}
9 NonAggr1(int, int) { } // expected-note {{candidate constructor}}
15 struct NonAggr2
: public Base
{ // expected-note 0-3 {{candidate constructor}}
19 class NonAggr3
{ // expected-note 3 {{candidate constructor}}
23 struct NonAggr4
{ // expected-note 3 {{candidate constructor}}
28 NonAggr1 na1
= { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr1'}}
29 NonAggr2 na2
= { 17 };
30 NonAggr3 na3
= { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr3'}}
31 NonAggr4 na4
= { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr4'}}
32 #if __cplusplus <= 201402L
33 // expected-error@-4{{no matching constructor for initialization of 'NonAggr2'}}
35 // expected-error@-6{{requires explicit braces}}
36 NonAggr2 na2b
= { {}, 17 }; // ok
40 typedef int type
[][2];
43 // Vector initialization.
44 typedef short __v4hi
__attribute__ ((__vector_size__ (8)));
45 __v4hi v1
= { (void *)1, 2, 3 }; // expected-error {{cannot initialize a vector element of type 'short' with an rvalue of type 'void *'}}
47 // Array initialization.
48 int a
[] = { (void *)1 }; // expected-error {{cannot initialize an array element of type 'int' with an rvalue of type 'void *'}}
50 // Struct initialization.
51 struct S
{ int a
; } s
= { (void *)1 }; // expected-error {{cannot initialize a member subobject of type 'int' with an rvalue of type 'void *'}}
53 // Check that we're copy-initializing the structs.
59 A(const A
&) = delete; // expected-note 0-2{{'A' has been explicitly marked deleted here}}
73 #if __cplusplus <= 201402L
74 // expected-error@-2 {{copying array element of type 'A' invokes deleted constructor}}
79 #if __cplusplus <= 201402L
80 // expected-error@-2 {{copying member subobject of type 'A' invokes deleted constructor}}
97 AggAgg aggagg
= { 1, 2, 3, 4 };
99 namespace diff_cpp14_dcl_init_aggr_example
{
102 friend struct derived
;
106 struct derived
: base
{};
109 #if __cplusplus > 201402L
110 // expected-error@-2 {{private}}
111 // expected-note@-7 {{here}}
116 namespace ProtectedBaseCtor
{
117 // FIXME: It's unclear whether f() and g() should be valid in C++1z. What is
118 // the object expression in a constructor call -- the base class subobject or
119 // the complete object?
125 struct B
: public A
{
132 #if __cplusplus > 201402L
133 // expected-error@-2 {{protected default constructor}}
134 // expected-note@-12 {{here}}
137 B
g() { return {{}}; }
138 #if __cplusplus <= 201402L
139 // expected-error@-2 {{no matching constructor}}
140 // expected-note@-15 3{{candidate}}
142 // expected-error@-5 {{protected default constructor}}
143 // expected-note@-21 {{here}}
146 B
h() { return {A
{}}; }
147 #if __cplusplus <= 201402L
148 // expected-error@-2 {{no matching constructor}}
149 // expected-note@-24 3{{candidate}}
151 // expected-error@-5 {{protected constructor}}
152 // expected-note@-30 {{here}}
155 namespace IdiomaticStdArrayInitDoesNotWarn
{
156 #pragma clang diagnostic push
157 #pragma clang diagnostic warning "-Wmissing-braces"
158 template<typename T
, int N
> struct StdArray
{
161 StdArray
<int, 3> x
= {1, 2, 3};
163 template<typename T
, int N
> struct ArrayAndSomethingElse
{
167 ArrayAndSomethingElse
<int, 3> y
= {1, 2, 3}; // expected-warning {{suggest braces}}
169 #if __cplusplus >= 201703L
170 template<typename T
, int N
> struct ArrayAndBaseClass
: StdArray
<int, 3> {
173 ArrayAndBaseClass
<int, 3> z
= {1, 2, 3}; // expected-warning {{suggest braces}}
175 // This pattern is used for tagged aggregates and must not warn
176 template<typename T
, int N
> struct JustABaseClass
: StdArray
<T
, N
> {};
177 JustABaseClass
<int, 3> w
= {1, 2, 3};
178 // but this should be also ok
179 JustABaseClass
<int, 3> v
= {{1, 2, 3}};
181 template <typename T
, int N
> struct OnionBaseClass
: JustABaseClass
<T
, N
> {};
182 OnionBaseClass
<int, 3> u
= {1, 2, 3};
183 OnionBaseClass
<int, 3> t
= {{{1, 2, 3}}};
187 template <typename T
, int N
> struct AggregateAndEmpty
: StdArray
<T
, N
>, EmptyBase
{};
188 AggregateAndEmpty
<int, 3> p
= {1, 2, 3}; // expected-warning {{suggest braces}}
191 #pragma clang diagnostic pop
194 namespace HugeArraysUseArrayFiller
{
195 // All we're checking here is that initialization completes in a reasonable
197 struct A
{ int n
; int arr
[1000 * 1000 * 1000]; } a
= {1, {2}};
200 namespace ElementDestructor
{
201 // The destructor for each element of class type is potentially invoked
202 // (15.4 [class.dtor]) from the context where the aggregate initialization
203 // occurs. Produce a diagnostic if an element's destructor isn't accessible.
205 class X
{ int f
; ~X(); }; // expected-note {{implicitly declared private here}}
209 auto *y
= new Y
{}; // expected-error {{temporary of type 'X' has private destructor}}
212 struct S0
{ int f
; ~S0() = delete; }; // expected-note 3 {{'~S0' has been explicitly marked deleted here}}
213 struct S1
{ S0 s0
; int f
; };
216 auto *t
= new S1
{ .f
= 1 }; // expected-error {{attempt to use a deleted function}}
217 return {2}; // expected-error {{attempt to use a deleted function}}
220 // Check if the type of an array element has a destructor.
221 struct S2
{ S0 a
[4]; };
224 auto *t
= new S2
{1,2,3,4}; // expected-error {{attempt to use a deleted function}}
227 #if __cplusplus >= 201703L
228 namespace BaseDestructor
{
229 struct S0
{ int f
; ~S0() = delete; }; // expected-note {{'~S0' has been explicitly marked deleted here}}
231 // Check destructor of base class.
235 S3 s3
= {1}; // expected-error {{attempt to use a deleted function}}
240 // A's destructor doesn't have to be accessible from the context of C's
242 struct A
{ friend struct B
; private: ~A(); };
243 struct B
{ B(); A a
; };