xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/aggregate-initialization.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // Verify that using an initializer list for a non-aggregate looks for
4*f4a2713aSLionel Sambuc // constructors..
5*f4a2713aSLionel Sambuc // Note that due to a (likely) standard bug, this is technically an aggregate,
6*f4a2713aSLionel Sambuc // but we do not treat it as one.
7*f4a2713aSLionel Sambuc struct NonAggr1 { // expected-note 2 {{candidate constructor}}
8*f4a2713aSLionel Sambuc   NonAggr1(int, int) { } // expected-note {{candidate constructor}}
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc   int m;
11*f4a2713aSLionel Sambuc };
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc struct Base { };
14*f4a2713aSLionel Sambuc struct NonAggr2 : public Base { // expected-note 3 {{candidate constructor}}
15*f4a2713aSLionel Sambuc   int m;
16*f4a2713aSLionel Sambuc };
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc class NonAggr3 { // expected-note 3 {{candidate constructor}}
19*f4a2713aSLionel Sambuc   int m;
20*f4a2713aSLionel Sambuc };
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc struct NonAggr4 { // expected-note 3 {{candidate constructor}}
23*f4a2713aSLionel Sambuc   int m;
24*f4a2713aSLionel Sambuc   virtual void f();
25*f4a2713aSLionel Sambuc };
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc NonAggr1 na1 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr1'}}
28*f4a2713aSLionel Sambuc NonAggr2 na2 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr2'}}
29*f4a2713aSLionel Sambuc NonAggr3 na3 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr3'}}
30*f4a2713aSLionel Sambuc NonAggr4 na4 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr4'}}
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc // PR5817
33*f4a2713aSLionel Sambuc typedef int type[][2];
34*f4a2713aSLionel Sambuc const type foo = {0};
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc // Vector initialization.
37*f4a2713aSLionel Sambuc typedef short __v4hi __attribute__ ((__vector_size__ (8)));
38*f4a2713aSLionel Sambuc __v4hi v1 = { (void *)1, 2, 3 }; // expected-error {{cannot initialize a vector element of type 'short' with an rvalue of type 'void *'}}
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // Array initialization.
41*f4a2713aSLionel Sambuc int a[] = { (void *)1 }; // expected-error {{cannot initialize an array element of type 'int' with an rvalue of type 'void *'}}
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc // Struct initialization.
44*f4a2713aSLionel Sambuc struct S { int a; } s = { (void *)1 }; // expected-error {{cannot initialize a member subobject of type 'int' with an rvalue of type 'void *'}}
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc // Check that we're copy-initializing the structs.
47*f4a2713aSLionel Sambuc struct A {
48*f4a2713aSLionel Sambuc   A();
49*f4a2713aSLionel Sambuc   A(int);
50*f4a2713aSLionel Sambuc   ~A();
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   A(const A&) = delete; // expected-note 2 {{function has been explicitly marked deleted here}}
53*f4a2713aSLionel Sambuc };
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc struct B {
56*f4a2713aSLionel Sambuc   A a;
57*f4a2713aSLionel Sambuc };
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc struct C {
60*f4a2713aSLionel Sambuc   const A& a;
61*f4a2713aSLionel Sambuc };
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc void f() {
64*f4a2713aSLionel Sambuc   A as1[1] = { };
65*f4a2713aSLionel Sambuc   A as2[1] = { 1 }; // expected-error {{copying array element of type 'A' invokes deleted constructor}}
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc   B b1 = { };
68*f4a2713aSLionel Sambuc   B b2 = { 1 }; // expected-error {{copying member subobject of type 'A' invokes deleted constructor}}
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc   C c1 = { 1 };
71*f4a2713aSLionel Sambuc }
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc class Agg {
74*f4a2713aSLionel Sambuc public:
75*f4a2713aSLionel Sambuc   int i, j;
76*f4a2713aSLionel Sambuc };
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc class AggAgg {
79*f4a2713aSLionel Sambuc public:
80*f4a2713aSLionel Sambuc   Agg agg1;
81*f4a2713aSLionel Sambuc   Agg agg2;
82*f4a2713aSLionel Sambuc };
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc AggAgg aggagg = { 1, 2, 3, 4 };
85