I'm comparing the behavior of constexpr and consteval:
#include <iostream>
#include <string_view>
[[nodiscard]] constexpr std::string_view DispCExpr(int) noexcept {
if (std::is_constant_evaluated()) {
return "consteval\n";
} else {
return "not consteval\n";
}
}
[[nodiscard]] consteval std::string_view DispCEval(int) noexcept {
// in consteval function std::is_const_evaluated() is always true
if (std::is_constant_evaluated()) {
return "consteval\n";
} else {
return "not consteval\n";
}
}
int main(int argc, char**) {
constexpr std::string_view sv1 = DispCExpr(2);
std::cout << sv1; // constant evaluated
std::string_view sv2 = DispCExpr(2);
std::cout << sv2; // not constant evaluated!
std::string_view sv3 = DispCEval(2);
std::cout << sv3; // constant evaluated!
// KO: normal argc not known at compile time
// std::string_view sv4 = DispCEval(argc);
// std::cout << sv4; // constant evaluated
}
Calling the constexpr function DispCExpr in order to initialize a constexpr variable actually makes it constant evaluated (std::is_constant_evaluated returns true).
But calling it, with a compile-time known constant in order to initialize a non-constexpr variable seems not to be a manifestly constant-evaluated expressions.
On the other hand, the consteval function DispCEval is always constant evaluated (as the compiler is warning me about live).
Why is there, what seem an inconsistency to me, between the sv2 and the sv3 situation?
How to understand the definition of "manifestly constant-evaluated"? seems to address the issue of a function call with constant but I'm failing to understand the difference between manifestly constant-evaluated expressions and such a call.