| 1 | // Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | |
| 20 | #include <type_traits> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | template<int N> |
| 24 | struct X { int v = N; }; |
| 25 | X<std::is_constant_evaluated()> x; // type X<true> |
| 26 | int y = 4; |
| 27 | int a = std::is_constant_evaluated() ? y : 1; // initializes a to 1 |
| 28 | int b = std::is_constant_evaluated() ? 2 : y; // initializes b to 2 |
| 29 | int c = y + (std::is_constant_evaluated() ? 2 : y); // initializes c to 2*y |
| 30 | int d = std::is_constant_evaluated(); // initializes d to 1 |
| 31 | int e = d + std::is_constant_evaluated(); // initializes e to 1 + 0 |
| 32 | |
| 33 | constexpr int |
| 34 | foo(int x) |
| 35 | { |
| 36 | const int n = std::is_constant_evaluated() ? 13 : 17; // n == 13 |
| 37 | int m = std::is_constant_evaluated() ? 13 : 17; // m might be 13 or 17 (see below) |
| 38 | char arr[n] = {}; // char[13] |
| 39 | return m + sizeof (arr) + x; |
| 40 | } |
| 41 | |
| 42 | constexpr int |
| 43 | bar() |
| 44 | { |
| 45 | const int n = std::is_constant_evaluated() ? 13 : 17; |
| 46 | X<n> x1; |
| 47 | X<std::is_constant_evaluated() ? 13 : 17> x2; |
| 48 | static_assert(std::is_same<decltype(x1), decltype(x2)>::value, |
| 49 | "x1/x2's type"); |
| 50 | return x1.v + x2.v; |
| 51 | } |
| 52 | |
| 53 | int p = foo(0); // m == 13; initialized to 26 |
| 54 | int q = p + foo(0); // m == 17 for this call; initialized to 56 |
| 55 | static_assert(bar() == 26, "bar"); |
| 56 | |
| 57 | struct S { int a, b; }; |
| 58 | |
| 59 | S s = { std::is_constant_evaluated() ? 2 : 3, y }; |
| 60 | S t = { std::is_constant_evaluated() ? 2 : 3, 4 }; |
| 61 | |
| 62 | static_assert(std::is_same<decltype(x), X<true> >::value, "x's type"); |
| 63 | |
| 64 | void |
| 65 | test01() |
| 66 | { |
| 67 | VERIFY( a == 1 && b == 2 && c == 8 && d == 1 && e == 1 && p == 26 ); |
| 68 | VERIFY( q == 56 && s.a == 3 && s.b == 4 && t.a == 2 && t.b == 4 ); |
| 69 | VERIFY( foo (y) == 34 ); |
| 70 | if constexpr (foo (0) != 26) |
| 71 | VERIFY( 0 ); |
| 72 | constexpr int w = foo (0); |
| 73 | VERIFY( w == 26 ); |
| 74 | } |
| 75 | |
| 76 | int main() |
| 77 | { |
| 78 | test01(); |
| 79 | } |