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