Is there any difference between constexpr
and consteval
?
consteval int x1 = 2;
constexpr int x2 = 5;
Is it better to use constexpr than consteval?
Is there any difference between constexpr
and consteval
?
consteval int x1 = 2;
constexpr int x2 = 5;
Is it better to use constexpr than consteval?
The standard actually says:
So your first example should not compile.
However, you can put
consteval
orconstexpr
on functions:constexpr
when applied to a function ismerelylargely advisory (see Davis Herring's comment) - it says to the compiler, evaluate this function call at compile time if you can.consteval
, on the other hand, is mandatory - it says to the compiler, generate an error if you can't evaluate this function call at compile time.Live demo