Debian Codegraph @

clang/test/SemaCXX/cxx2c-decomposition.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected
2 // RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected -fexperimental-new-constant-interpreter
3
4 namespace std {
5 using size_t = decltype(sizeof(0));
6 template<typename> struct tuple_size;
7 template<size_t, typename> struct tuple_element;
8 }
9
10 struct Y { int n = 0; };
11 struct X { X(); X(Y); X(const X&); ~X(); int k = 42;}; // #X-decl
12 struct Z { constexpr Z(): i (43){}; int i;}; // #Z-decl
13 struct Z2 { constexpr Z2(): i (0){}; int i; ~Z2();}; // #Z2-decl
14
15 struct Bit { constexpr Bit(): i(1), j(1){}; int i: 2; int j:2;};
16
17 struct A { int a : 13; bool b; };
18
19 struct B {};
20 template<> struct std::tuple_size<B> { enum { value = 2 }; };
21 template<> struct std::tuple_size<const B> { enum { value = 2 }; };
22 template<> struct std::tuple_element<0, const B> { using type = Y; };
23 template<> struct std::tuple_element<1, const B> { using type = const int&; };
24 template<int N>
25 constexpr auto get(B) {
26 if constexpr (N == 0)
27 return Y();
28 else
29 return 0.0;
30 }
31
32
33 constexpr auto [t1] = Y {42};
34 static_assert(t1 == 42);
35
36 constexpr int i[] = {1, 2};
37 constexpr auto [t2, t3] = i;
38 static_assert(t2 == 1);
39 static_assert(t3 == 2);
40
41 constexpr auto [t4] = X();
42 // expected-error@-1 {{constexpr variable cannot have non-literal type 'const X'}} \
43 // expected-note@#X-decl {{'X' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors}}
44
45 constexpr auto [t5] = Z();
46 static_assert(t5 == 43);
47
48 constexpr auto [t6] = Z2();
49 //expected-error@-1 {{constexpr variable cannot have non-literal type 'const Z2'}}
50 // expected-note@#Z2-decl {{'Z2' is not literal because its destructor is not constexpr}}
51
52 constexpr auto [t7, t8] = Bit();
53 static_assert(t7 == 1);
54 static_assert(t8 == 1);
55
56 void test_tpl(auto) {
57 constexpr auto [...p] = Bit();
58 static_assert(((p == 1) && ...));
59 }
60
61 void test() {
62 test_tpl(0);
63 }
64
65 // FIXME : support tuple
66 constexpr auto [a, b] = B{};
67 static_assert(a.n == 0);
68 // expected-error@-1 {{static assertion expression is not an integral constant expression}} \
69 // expected-note@-1 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}\
70 // expected-note@-2 {{temporary created here}}
71
72 constinit auto [init1] = Y {42};
73 constinit auto [init2] = X {}; // expected-error {{variable does not have a constant initializer}} \
74 // expected-note {{required by 'constinit' specifier here}} \
75 // expected-note {{non-constexpr constructor 'X' cannot be used in a constant expression}} \
76 // expected-note@#X-decl {{declared here}}