| 1 |
// P2564R3 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
// Some of these were cribbed from clang's cxx2b-consteval-propagate.cpp. |
| 4 |
|
| 5 |
consteval int id(int i) { return i; } |
| 6 |
|
| 7 |
template <typename T> |
| 8 |
constexpr int |
| 9 |
f0 (T t) |
| 10 |
{ |
| 11 |
// OK, f0<int> promoted to consteval. |
| 12 |
return id (t); // { dg-message "immediate-escalating expression .id\\(t\\)." } |
| 13 |
} |
| 14 |
|
| 15 |
constexpr auto a0 = f0 (3); |
| 16 |
|
| 17 |
// As a consequence of f0<int> being promoted to an immediate function, we |
| 18 |
// can't take its address. |
| 19 |
auto p0 = &f0<int>; // { dg-error "taking address of an immediate function" } |
| 20 |
|
| 21 |
template <typename T> |
| 22 |
constexpr int |
| 23 |
f1 (T t) |
| 24 |
{ |
| 25 |
// OK, f1<int> promoted to consteval. |
| 26 |
return t + id (t); // { dg-message "immediate-escalating expression .id\\(t\\)." } |
| 27 |
} |
| 28 |
|
| 29 |
constexpr auto a1 = f1 (3); |
| 30 |
|
| 31 |
// As a consequence of f1<int> being promoted to an immediate function, we |
| 32 |
// can't take its address. |
| 33 |
auto p1 = &f1<int>; // { dg-error "taking address of an immediate function" } |
| 34 |
|
| 35 |
template <typename T> |
| 36 |
constexpr int |
| 37 |
f2 (T) |
| 38 |
{ |
| 39 |
// This produces a constant; f2 *not* promoted to consteval. |
| 40 |
return id (42); |
| 41 |
} |
| 42 |
|
| 43 |
// ... so we can take its address. |
| 44 |
auto p2 = &f2<int>; |
| 45 |
|
| 46 |
constexpr int |
| 47 |
f3 (int i) |
| 48 |
{ |
| 49 |
// f3 isn't a function template and those don't get upgraded to consteval. |
| 50 |
return id (i); // { dg-error "not a constant expression" } |
| 51 |
} |
| 52 |
|
| 53 |
auto p3 = &f3; |
| 54 |
|
| 55 |
template<typename T> |
| 56 |
constexpr int |
| 57 |
f4 (T t) |
| 58 |
{ |
| 59 |
auto p = id; // { dg-message "immediate-escalating expression .id." } |
| 60 |
(void) p; |
| 61 |
return t; |
| 62 |
} |
| 63 |
|
| 64 |
auto p6 = &f4<int>; // { dg-error "taking address of an immediate function" } |
| 65 |
|
| 66 |
static_assert (f4 (42) == 42); |
| 67 |
|
| 68 |
// Constructors. |
| 69 |
consteval int zero (int) |
| 70 |
{ |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
struct A { |
| 75 |
// A::A(auto) promoted to consteval. |
| 76 |
constexpr A(auto i) { zero (i); } |
| 77 |
}; |
| 78 |
|
| 79 |
constexpr void |
| 80 |
f5 (auto i) |
| 81 |
{ |
| 82 |
A a{i}; |
| 83 |
} |
| 84 |
|
| 85 |
constexpr void |
| 86 |
f5_nt (int i) |
| 87 |
{ |
| 88 |
A a{i}; // { dg-error "call to consteval function|not a constant" } |
| 89 |
} |
| 90 |
|
| 91 |
void |
| 92 |
f6 () |
| 93 |
{ |
| 94 |
f5 (0); |
| 95 |
} |
| 96 |
|
| 97 |
struct B { |
| 98 |
constexpr B(int) { } |
| 99 |
}; |
| 100 |
|
| 101 |
B b1(f0<int>((f1<int>(7)))); |
| 102 |
|
| 103 |
template<typename T> |
| 104 |
constexpr int cid(T t) { return t; } |
| 105 |
|
| 106 |
auto p4 = &cid<int>; |
| 107 |
auto p5 = &cid<char>; |
| 108 |
|
| 109 |
int g = 7; // { dg-message ".int g. is not const" } |
| 110 |
|
| 111 |
B b2(f0<int>(cid<int>(g))); // { dg-error "call to consteval function|not usable" } |
| 112 |
|
| 113 |
struct C { |
| 114 |
consteval C (int) {}; |
| 115 |
}; |
| 116 |
|
| 117 |
constexpr int |
| 118 |
f7 (auto t) |
| 119 |
{ |
| 120 |
C c(t); // { dg-message "immediate-escalating expression .c.C::C\\(t\\)." } |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
int i1 = f7 (g); // { dg-error "call to consteval function|not usable" } |
| 125 |
|
| 126 |
struct Y { |
| 127 |
int y; |
| 128 |
int x = id (y); |
| 129 |
consteval Y (int i) : y (id (i)) {} |
| 130 |
}; |
| 131 |
|
| 132 |
Y y1(1); |
| 133 |
Y y2(g); // { dg-error "call to consteval function|not usable" } |
| 134 |
|
| 135 |
struct Y2 { |
| 136 |
int y; |
| 137 |
int x = id (y); |
| 138 |
constexpr Y2 (auto i) : y (id (i)) {} |
| 139 |
}; |
| 140 |
|
| 141 |
Y2 y3(1); |
| 142 |
Y2 y4(g); // { dg-error "call to consteval function|not usable" } |
| 143 |
|
| 144 |
auto l1 = [](int i) constexpr { |
| 145 |
int t = id (i); |
| 146 |
return id (0); |
| 147 |
}; |
| 148 |
|
| 149 |
int (*pl1)(int) = l1; // { dg-error "call to consteval function|returns address of immediate function" } |
| 150 |
|
| 151 |
auto l2 = [](int i) { |
| 152 |
int t = id (i); |
| 153 |
return id (0); |
| 154 |
}; |
| 155 |
|
| 156 |
int (*pl2)(int) = l2; // { dg-error "call to consteval function|returns address of immediate function" } |
| 157 |
|
| 158 |
// Not defined = won't produce a constant expression. |
| 159 |
consteval int undef (); // { dg-warning "used but never defined" } |
| 160 |
|
| 161 |
struct S { |
| 162 |
int a = [] { return undef (); }(); |
| 163 |
}; |
| 164 |
|
| 165 |
struct S2 { // { dg-error "used before its definition" } |
| 166 |
int a = [] (int u = undef ()) { |
| 167 |
return u; |
| 168 |
}(); |
| 169 |
} s2; // { dg-error "call to consteval function" } |