C++20 will have the new consteval keyword and constexpr destructor if all goes well. Sadly no compiler that I know of implements consteval at this time. Will the following code be valid?
struct A {
constexpr ~A() {}
};
consteval A f() {
return A{};
}
void test() {
A a;
a = f(); // <-- here
}
The issue comes from the line I marked — the destructor of the temporary A returned by f need to be called. but it should be called at the end of the full expression. so outside of the immediate evaluation.
I didn't find any quotes from the consteval and constexpr destructor papers that explicitly forbid this, but I can't see how it could be correct.
Will the following code be valid in C++20? What should happen with this code?
Note:
In the consteval paper, this example is given. Here the consteval function is called outside of constant context.
consteval int sqr(int n) {
return n*n;
}
constexpr int r = sqr(100); // Okay.
int x = 100;
int r2 = sqr(x); // Error: Call does not produce a constant.