| 1 |
// P2564R3 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
// { dg-options "-Wno-c++23-extensions" } |
| 4 |
|
| 5 |
consteval int zero (int) |
| 6 |
{ |
| 7 |
return 0; |
| 8 |
} |
| 9 |
|
| 10 |
struct A { |
| 11 |
// A::A(auto) promoted to consteval. |
| 12 |
constexpr A(auto i) { zero (i); } |
| 13 |
}; |
| 14 |
|
| 15 |
// 'f1<int>' is an immediate function because its body contains a call to an |
| 16 |
// immediate constructor 'A<int>' and that call is not a constant expression |
| 17 |
constexpr void |
| 18 |
f1 (auto i) |
| 19 |
{ |
| 20 |
A a{i}; |
| 21 |
} |
| 22 |
|
| 23 |
// 'f2<int>' is an immediate function because its body contains a call to an |
| 24 |
// immediate constructor 'A<int>' and that call is not a constant expression |
| 25 |
constexpr void |
| 26 |
f2 (auto i) |
| 27 |
{ |
| 28 |
A a{i}; |
| 29 |
} |
| 30 |
|
| 31 |
void |
| 32 |
f3 (int i) |
| 33 |
{ |
| 34 |
A a{i}; // { dg-error "not a constant expression" } |
| 35 |
} |
| 36 |
|
| 37 |
inline void |
| 38 |
f7 (int i) |
| 39 |
{ |
| 40 |
A a{i}; // { dg-error "not a constant expression" } |
| 41 |
} |
| 42 |
|
| 43 |
constexpr void |
| 44 |
f8 (int i) |
| 45 |
{ |
| 46 |
A a{i}; // { dg-error "not a constant expression" } |
| 47 |
} |
| 48 |
|
| 49 |
/* "An expression or conversion is immediate-escalating if it is not initially |
| 50 |
in an immediate function context" but this one is, so we do *not* promote |
| 51 |
f4 to consteval. */ |
| 52 |
constexpr void |
| 53 |
f4 (auto i) |
| 54 |
{ |
| 55 |
if consteval { |
| 56 |
A a{i}; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
constexpr void |
| 61 |
f5 (auto i) |
| 62 |
{ |
| 63 |
if not consteval { |
| 64 |
(void) 0; |
| 65 |
} else { |
| 66 |
A a{i}; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
f6 (int x) |
| 72 |
{ |
| 73 |
f1 (0); |
| 74 |
f1 (x); // { dg-error "not a constant expression" } |
| 75 |
f2 (0); |
| 76 |
f2 (x); // { dg-error "not a constant expression" } |
| 77 |
f3 (0); |
| 78 |
f4 (x); |
| 79 |
f4 (0); |
| 80 |
f5 (x); |
| 81 |
f5 (0); |
| 82 |
} |