| 1 |
// P2564R3 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
// The problem here was that while parsing, we first process calling |
| 4 |
// 'f' from 'g' but only when instantiating 'f<int>' do we promote 'f' |
| 5 |
// to consteval. When the var we're initializing is marked constexpr, |
| 6 |
// store_init_value detects the problem that we're calling a consteval |
| 7 |
// function with non-const argument. |
| 8 |
|
| 9 |
consteval int id(int i) { return i; } |
| 10 |
|
| 11 |
// Don't let the instantiations confuse us, e.g. instantiating a fn |
| 12 |
// prior to entering 'g'. |
| 13 |
template <typename T> |
| 14 |
constexpr int f1(T t) { return id (t); } |
| 15 |
|
| 16 |
template <typename T> |
| 17 |
constexpr int f2(T t) { return id (t); } |
| 18 |
|
| 19 |
template <typename T> |
| 20 |
constexpr int f3(T t) { return id (t); } |
| 21 |
|
| 22 |
template <typename T> |
| 23 |
constexpr int f4(T t) { return id (t); } |
| 24 |
|
| 25 |
template <typename T> |
| 26 |
constexpr int f5(T t) { return id (t); } |
| 27 |
|
| 28 |
template <typename T> |
| 29 |
constexpr int f6(T t) { return id (t); } |
| 30 |
|
| 31 |
template <typename T> |
| 32 |
constexpr int f7(T t) { return id (t); } |
| 33 |
|
| 34 |
template <typename T> |
| 35 |
constexpr int f8(T t) { return id (t); } |
| 36 |
|
| 37 |
template <typename T> |
| 38 |
constexpr int f9(T t) { return id (t); } |
| 39 |
|
| 40 |
template <typename T> |
| 41 |
constexpr int f10(T t) { return id (t); } |
| 42 |
|
| 43 |
template <typename T> |
| 44 |
constexpr int g1(T t) { auto p = id; return p (t); } |
| 45 |
|
| 46 |
int non_const; |
| 47 |
|
| 48 |
auto a1 = f1 (non_const); // { dg-error "call to consteval function|not usable" } |
| 49 |
constexpr auto a2 = f2 (non_const); // { dg-error "not a constant|not usable" } |
| 50 |
auto a3 = f3 (42); |
| 51 |
constexpr auto a4 = f4 (42); |
| 52 |
|
| 53 |
void |
| 54 |
g () |
| 55 |
{ |
| 56 |
auto a5 = f5 (non_const); // { dg-error "not a constant|not usable" } |
| 57 |
constexpr auto a6 = f6 (non_const); // { dg-error "not usable" } |
| 58 |
auto a7 = f7 (42); |
| 59 |
constexpr auto a8 = f8 (42); |
| 60 |
(void) f9 (non_const); // { dg-error "not a constant|not usable" } |
| 61 |
(void) f10 (42); |
| 62 |
(void) g1 (non_const); // { dg-error "not a constant|not usable" } |
| 63 |
} |
| 64 |
|
| 65 |
struct S { |
| 66 |
int y; |
| 67 |
int x = id (y); |
| 68 |
// Promoted to consteval. |
| 69 |
template<typename T> |
| 70 |
constexpr S(T t) : y (id (t)) {} |
| 71 |
}; |
| 72 |
|
| 73 |
S s1(1); |
| 74 |
S s2(non_const); // { dg-error "call to consteval function|not usable" } |
| 75 |
constexpr S s3(1); |
| 76 |
constexpr S s4(non_const); // { dg-error "not usable" } |