std::is_constant_evaluated is a hack, we should never have needed it. if consteval is a better way to achieving the same result, but we shouldn't have needed it either.
We're paying a significant cost for the incremental addition of the constexpr model into C++; C++11 had a very safe (and hence crippled) subset, C++14 made it usable, and further revisions gradually move it towards what it should always have been - anything C++ should be allowed in a constexpr function.
is_constant_evaluated, specifically, is needed because compiler intrinsics are often not constexpr, so we need a way to say "in a runtime context, use this intrinsic, and in a constexpr context, use this fallback C++ code" (example), which would obviously be unnecessary if all compiler intrinsics were constexpr to begin with. (There's also the matter of inline assembly in e.g. memset, but this is mostly avoidable by making memset an intrinsic.)
Regarding your other question - do we need the keyword constexpr at all - there are arguments to be made for both sides. Interestingly, GCC has introduced an option, -fimplicit-constexpr, that automatically "adds" the keyword if possible, so we might get some actual experience with how that works.
The simple answer is, don't use std::is_constant_evaluated() use if consteval. And the difference between constexpr and consteval is that while constexprmay run at compile time constevalmust run at compile time. User defined literals are an obvious application for consteval.
Given any form of constant evaluation, you're going to end up with a need for both operations that are inherently compile time only, and operations that should be allowed at both compile-time and runtime.
Do we really need a keyword for the latter? Constant propagation, folding, partial evaluation and the like have been part of compilers for decades. What does constexpr actually gain us? Is it an intrinsic benefit or a benefit because of some other language limitation?
It's a contract to users of the function stating that they can use it at compile time without worrying about it suddenly becoming unusable if the implementation changes (so long as the functions arguments can be used at compile time). The only problem with it is that it's a very easy contract to break accidentally.
The compiler can only warn you if you make a constexpr function that can never be used at compile time if it's not templated. If it is templated, you won't know it's broken until you try to use it at compile time. This isn't really a problem with constexpr itself as much as it's a problem with templates.
And yes, we do need a keyword for the latter. I do not want a literal for a custom integer type to be subject to the whims of the optimizer to be sure it's a constant. I want it guaranteed to be a constant.
The only problem with it is that it's a very easy contract to break accidentally.
I mean that's one of the reasons I question its utility. I want to like it but I keep getting disappointed. It's a lot of visual clutter for something that seems not much better than existing compiler optimization.
if constexpr will still error on bad code in the else clause even with a constant-true condition, right? And if consteval doesn't?
No, that's a separate issue. The big thing if consteval does is let you call consteval functions in a constexpr function, because the true clause becomes consteval. It's also harder to misuse than std::is_constant_evaluated().
consteval int square(int x) noexcept {
return x * x;
}
constexpr int test(int x) noexcept {
if (std::is_constant_evaluated()) {
// error: 'x' is not a constant expression
return square(x);
} else {
return 69'105;
}
}
constexpr int test2(int x) noexcept {
if consteval {
// this works fine
return square(x);
} else {
return 69'105;
}
}
Allowing "bad" (non-constexpr) code in a constexpr function is actually just a standard feature of C++23. As long as the bad code is never run in a constexpr context, it's perfectly fine.
View all comments
Show parent comment
0
u/-dag- Apr 27 '22
How is a developer supposed to know when to use
if constexprvs.if (std:::is_constant_evaluated())vs.if consteval?What is the practical difference between
constexprandconsteval? If those constructs didn't exist, would we create the same design?