1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=expected,both %s 2 // RUN: %clang_cc1 -std=c++17 -verify=ref,both %s 3 4 struct F { int a; int b;}; 5 constexpr F getF() { 6 return {12, 3}; 7 } 8 constexpr int f() { 9 auto [a1, b1] = getF(); 10 auto [a2, b2] = getF(); 11 12 return a1 + a2 + b1 + b2; 13 } 14 static_assert(f() == 30); 15 16 17 constexpr int structRefs() { 18 const auto &[a, b] = getF(); 19 20 return a + b; 21 } 22 static_assert(structRefs() == 15); 23 24 constexpr int structRefs2() { 25 F f = getF(); 26 const auto &[a, b] = f; 27 28 return a + b; 29 } 30 static_assert(structRefs2() == 15); 31 32 33 template<typename T> 34 struct Tuple { 35 T first; 36 T second; 37 constexpr Tuple(T a, T b) : first(a), second(b) {} 38 }; 39 template<typename T> 40 constexpr T addTuple(const Tuple<T> &Tu) { 41 auto [a, b] = Tu; 42 return a + b; 43 } 44 45 template<typename T> 46 constexpr T addTuple2(const Tuple<T> &Tu) { 47 auto [a, b] = Tu; 48 return Tu.first + Tu.second; 49 } 50 51 constexpr Tuple<int> T1 = Tuple(1,2); 52 static_assert(addTuple(T1) == 3); 53 static_assert(addTuple2(T1) == 3); 54 55 constexpr Tuple<short> T2 = Tuple<short>(11,2); 56 static_assert(addTuple(T2) == 13); 57 static_assert(addTuple2(T2) == 13); 58 59 constexpr int Modify() { 60 auto T = Tuple<int>(10, 20); 61 auto &[x, y] = T; 62 x += 1; 63 y += 1; 64 return T.first + T.second; 65 } 66 static_assert(Modify() == 32, ""); 67 68 constexpr int a() { 69 int a[2] = {5, 3}; 70 auto [x, y] = a; 71 return x + y; 72 } 73 static_assert(a() == 8); 74 75 constexpr int b() { 76 int a[2] = {5, 3}; 77 auto &[x, y] = a; 78 x += 1; 79 y += 2; 80 return a[0] + a[1]; 81 } 82 static_assert(b() == 11); 83 84 namespace cwg1872 { 85 template<typename T> struct A : T { 86 constexpr int f() const { return 0; } 87 }; 88 struct X {}; 89 struct Y { virtual int f() const; }; 90 struct Z : virtual X {}; 91 92 constexpr int z = A<Z>().f(); // both-error {{must be initialized by a constant expression}} \ 93 // both-note {{non-literal type 'A<Z>' cannot be used in a constant expression}} 94 } 95 96 /// The diagnostics between the two interpreters used to be different here. 97 struct S { int a; }; 98 constexpr S getS() { // both-error {{constexpr function never produces a constant expression}} 99 (void)(1/0); // both-note 2{{division by zero}} \ 100 // both-warning {{division by zero}} 101 return S{12}; 102 } 103 constexpr S s = getS(); // both-error {{must be initialized by a constant expression}} \ 104 // both-note {{in call to 'getS()'}} \ 105 // both-note {{declared here}} 106 static_assert(s.a == 12, ""); // both-error {{not an integral constant expression}} \ 107 // both-note {{initializer of 's' is not a constant expression}} 108 109 using size_t = decltype(sizeof(0)); 110 namespace std { template<typename T> struct tuple_size; } 111 namespace std { template<size_t, typename> struct tuple_element; } 112 113 namespace constant { 114 struct Q {}; 115 template<int N> constexpr int get(Q &&) { return N * N; } 116 } 117 template<> struct std::tuple_size<constant::Q> { static const int value = 3; }; 118 template<int N> struct std::tuple_element<N, constant::Q> { typedef int type; }; 119 120 namespace constant { 121 Q q; 122 constexpr bool f() { 123 auto [a, b, c] = q; 124 return a == 0 && b == 1 && c == 4; 125 } 126 static_assert(f()); 127 } 128