| 1 |
// P1938R3 |
| 2 |
// { dg-do run { target c++20 } } |
| 3 |
// { dg-options "" } |
| 4 |
|
| 5 |
extern "C" void abort (); |
| 6 |
|
| 7 |
namespace std { |
| 8 |
constexpr inline bool |
| 9 |
is_constant_evaluated () noexcept |
| 10 |
{ |
| 11 |
if consteval { // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 12 |
return true; |
| 13 |
} else { |
| 14 |
return false; |
| 15 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
consteval int foo (int x) { return x; } |
| 20 |
consteval int bar () { return 2; } |
| 21 |
|
| 22 |
constexpr int |
| 23 |
baz (int x) |
| 24 |
{ |
| 25 |
int r = 0; |
| 26 |
if consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 27 |
{ |
| 28 |
r += foo (x); |
| 29 |
} |
| 30 |
else |
| 31 |
{ |
| 32 |
r += bar (); |
| 33 |
} |
| 34 |
if ! consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 35 |
{ |
| 36 |
r += 2 * bar (); |
| 37 |
} |
| 38 |
else |
| 39 |
{ |
| 40 |
r += foo (8 * x); |
| 41 |
} |
| 42 |
if (std::is_constant_evaluated ()) |
| 43 |
r = -r; |
| 44 |
if consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 45 |
{ |
| 46 |
r += foo (32 * x); |
| 47 |
} |
| 48 |
if not consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 49 |
{ |
| 50 |
r += 32 * bar (); |
| 51 |
} |
| 52 |
return r; |
| 53 |
} |
| 54 |
|
| 55 |
template <typename T> |
| 56 |
constexpr int |
| 57 |
qux (T x) |
| 58 |
{ |
| 59 |
T r = 0; |
| 60 |
if consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 61 |
{ |
| 62 |
r += foo (x); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
r += bar (); |
| 67 |
} |
| 68 |
if ! consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 69 |
{ |
| 70 |
r += 2 * bar (); |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
r += foo (8 * x); |
| 75 |
} |
| 76 |
if (std::is_constant_evaluated ()) |
| 77 |
r = -r; |
| 78 |
if consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 79 |
{ |
| 80 |
r += foo (32 * x); |
| 81 |
} |
| 82 |
if not consteval // { dg-warning "'if consteval' only available with" "" { target c++20_only } } |
| 83 |
{ |
| 84 |
r += 32 * bar (); |
| 85 |
} |
| 86 |
return r; |
| 87 |
} |
| 88 |
|
| 89 |
constexpr int a = baz (1); |
| 90 |
static_assert (a == 23); |
| 91 |
int b = baz (1); |
| 92 |
constexpr int c = qux (1); |
| 93 |
static_assert (c == 23); |
| 94 |
int d = qux<int> (1); |
| 95 |
|
| 96 |
int |
| 97 |
main () |
| 98 |
{ |
| 99 |
if (b != 23 || d != 23) |
| 100 |
abort (); |
| 101 |
if (baz (1) != 70 || qux (1) != 70 || qux (1LL) != 70) |
| 102 |
abort (); |
| 103 |
} |