xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/cxx1y-initializer-aggregates.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++1y %s -verify
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc namespace in_class_init {
4*f4a2713aSLionel Sambuc   union U { char c; double d = 4.0; };
5*f4a2713aSLionel Sambuc   constexpr U u1 = U();
6*f4a2713aSLionel Sambuc   constexpr U u2 {};
7*f4a2713aSLionel Sambuc   constexpr U u3 { 'x' };
8*f4a2713aSLionel Sambuc   static_assert(u1.d == 4.0, "");
9*f4a2713aSLionel Sambuc   static_assert(u2.d == 4.0, "");
10*f4a2713aSLionel Sambuc   static_assert(u3.c == 'x', "");
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc   struct A {
13*f4a2713aSLionel Sambuc     int n = 5;
14*f4a2713aSLionel Sambuc     int m = n * 3;
15*f4a2713aSLionel Sambuc     union {
16*f4a2713aSLionel Sambuc       char c;
17*f4a2713aSLionel Sambuc       double d = 4.0;
18*f4a2713aSLionel Sambuc     };
19*f4a2713aSLionel Sambuc   };
20*f4a2713aSLionel Sambuc   constexpr A a1 {};
21*f4a2713aSLionel Sambuc   constexpr A a2 { 8 };
22*f4a2713aSLionel Sambuc   constexpr A a3 { 1, 2, { 3 } };
23*f4a2713aSLionel Sambuc   constexpr A a4 { 1, 2, { .d = 3.0 } };
24*f4a2713aSLionel Sambuc   static_assert(a1.d == 4.0, "");
25*f4a2713aSLionel Sambuc   static_assert(a2.m == 24, "");
26*f4a2713aSLionel Sambuc   static_assert(a2.d == 4.0, "");
27*f4a2713aSLionel Sambuc   static_assert(a3.c == 3, "");
28*f4a2713aSLionel Sambuc   static_assert(a3.d == 4.0, ""); // expected-error {{constant expression}} expected-note {{active member 'c'}}
29*f4a2713aSLionel Sambuc   static_assert(a4.d == 3.0, "");
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc   struct B {
32*f4a2713aSLionel Sambuc     int n;
fin_class_init::B33*f4a2713aSLionel Sambuc     constexpr int f() { return n * 5; }
34*f4a2713aSLionel Sambuc     int m = f();
35*f4a2713aSLionel Sambuc   };
36*f4a2713aSLionel Sambuc   B b1 {};
37*f4a2713aSLionel Sambuc   constexpr B b2 { 2 };
38*f4a2713aSLionel Sambuc   B b3 { 1, 2 };
39*f4a2713aSLionel Sambuc   static_assert(b2.m == 10, "");
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc   struct C {
42*f4a2713aSLionel Sambuc     int k;
43*f4a2713aSLionel Sambuc     union {
44*f4a2713aSLionel Sambuc       int l = k; // expected-error {{invalid use of non-static}}
45*f4a2713aSLionel Sambuc     };
46*f4a2713aSLionel Sambuc   };
47*f4a2713aSLionel Sambuc }
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc namespace nested_aggregate_init {
50*f4a2713aSLionel Sambuc   struct A {
51*f4a2713aSLionel Sambuc     int n = 5;
52*f4a2713aSLionel Sambuc     int b = n * 3;
53*f4a2713aSLionel Sambuc   };
54*f4a2713aSLionel Sambuc   struct B {
Bnested_aggregate_init::B55*f4a2713aSLionel Sambuc     constexpr B(int k) : d(1.23), k(k) {}
56*f4a2713aSLionel Sambuc     // Within this aggregate, both this object's 'this' and the temporary's
57*f4a2713aSLionel Sambuc     // 'this' are used.
fnested_aggregate_init::B58*f4a2713aSLionel Sambuc     constexpr int f() const { return A{k}.b; }
59*f4a2713aSLionel Sambuc     double d;
60*f4a2713aSLionel Sambuc     int k;
61*f4a2713aSLionel Sambuc   };
62*f4a2713aSLionel Sambuc   static_assert(B(6).f() == 18, "");
63*f4a2713aSLionel Sambuc }
64