| 1 |
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s |
| 2 |
// RUN: %clang_cc1 -verify=ref,both %s |
| 3 |
|
| 4 |
union U { |
| 5 |
int a; |
| 6 |
int b; |
| 7 |
}; |
| 8 |
|
| 9 |
constexpr U a = {12}; |
| 10 |
static_assert(a.a == 12, ""); |
| 11 |
static_assert(a.b == 0, ""); // both-error {{not an integral constant expression}} \ |
| 12 |
// both-note {{read of member 'b' of union with active member 'a'}} |
| 13 |
union U1 { |
| 14 |
int i; |
| 15 |
float f = 3.0f; |
| 16 |
}; |
| 17 |
constexpr U1 u1{}; |
| 18 |
static_assert(u1.f == 3.0, ""); |
| 19 |
static_assert(u1.i == 1, ""); // both-error {{not an integral constant expression}} \ |
| 20 |
// both-note {{read of member 'i' of union with active member 'f'}} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
union A { |
| 25 |
int a; |
| 26 |
double d; |
| 27 |
}; |
| 28 |
constexpr A aa = {1, 2.0}; // both-error {{excess elements in union initializer}} |
| 29 |
constexpr A ab = {.d = 1.0}; |
| 30 |
static_assert(ab.d == 1.0, ""); |
| 31 |
static_assert(ab.a == 1, ""); // both-error {{not an integral constant expression}} \ |
| 32 |
// both-note {{read of member 'a' of union with active member 'd'}} |
| 33 |
|
| 34 |
|
| 35 |
namespace Empty { |
| 36 |
union E {}; |
| 37 |
constexpr E e{}; |
| 38 |
} |
| 39 |
|
| 40 |
namespace SimpleStore { |
| 41 |
union A { |
| 42 |
int a; |
| 43 |
int b; |
| 44 |
}; |
| 45 |
constexpr int foo() { |
| 46 |
A a{.b = 4}; |
| 47 |
a.b = 10; |
| 48 |
return a.b; |
| 49 |
} |
| 50 |
static_assert(foo() == 10, ""); |
| 51 |
|
| 52 |
constexpr int empty() { |
| 53 |
A a{}; /// Just test that this works. |
| 54 |
return 10; |
| 55 |
} |
| 56 |
static_assert(empty() == 10, ""); |
| 57 |
} |
| 58 |
|
| 59 |
namespace ZeroInit { |
| 60 |
struct S { int m; }; |
| 61 |
union Z { |
| 62 |
float f; |
| 63 |
}; |
| 64 |
|
| 65 |
constexpr Z z{}; |
| 66 |
static_assert(z.f == 0.0, ""); |
| 67 |
} |