brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 99278c6 Raw
77 lines · cpp
1// RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected2// RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected -fexperimental-new-constant-interpreter3 4namespace std {5  using size_t = decltype(sizeof(0));6  template<typename> struct tuple_size;7  template<size_t, typename> struct tuple_element;8}9 10struct Y { int n = 0; };11struct X { X(); X(Y); X(const X&); ~X(); int k = 42;}; // #X-decl12struct Z { constexpr Z(): i (43){}; int i;}; // #Z-decl13struct Z2 { constexpr Z2(): i (0){}; int i; ~Z2();}; // #Z2-decl14 15struct Bit { constexpr Bit(): i(1), j(1){}; int i: 2; int j:2;};16 17struct A { int a : 13; bool b; };18 19struct B {};20template<> struct std::tuple_size<B> { enum { value = 2 }; };21template<> struct std::tuple_size<const B> { enum { value = 2 }; };22template<> struct std::tuple_element<0, const B> { using type = Y; };23template<> struct std::tuple_element<1, const B> { using type = const int&; };24template<int N>25constexpr auto get(B) {26  if constexpr (N == 0)27    return Y();28  else29    return 0.0;30}31 32 33constexpr auto [t1] = Y {42};34static_assert(t1 == 42);35 36constexpr int i[] = {1, 2};37constexpr auto [t2, t3] = i;38static_assert(t2 == 1);39static_assert(t3 == 2);40 41constexpr 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 45constexpr auto [t5] = Z();46static_assert(t5 == 43);47 48constexpr 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 52constexpr auto [t7, t8] = Bit();53static_assert(t7 == 1);54static_assert(t8 == 1);55 56void test_tpl(auto) {57    constexpr auto [...p] = Bit();58    static_assert(((p == 1) && ...));59}60 61void test() {62    test_tpl(0);63}64 65// FIXME : support tuple66constexpr auto [a, b] = B{};67static_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 72constinit auto [init1] = Y {42};73constinit 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}}77