C++ const vs constexpr
The cluster discusses the semantics, compile-time guarantees, optimizations, and differences between `const` and `constexpr` in C++, including debates on their practical uses, error handling, and comparisons to similar features in other languages.
Activity Over Time
Top Contributors
Keywords
Sample Comments
I guess so... This is reason for `consteval` and such.
Wouldn't const be checked at compile time?
Nothing you've demonstrated requires the use of constexpr. A const is perfectly suited for that.
It's like 'const' itself -- it's a tool for producing better error messages, not better final executables.
I guess you're not a fan of 'const' either? ;)
Does “const” mean “immutable” and “constexpr” mean “compile-time” in C++? That’s funny.
static const's may not be optimized to literals. constexpr is not fully supported.
Can we please have constexpr in C 2x to put an end to this madness?
Won't the compiler optimize it away even without constexpr or const? The compiler can see it doesn't get changed.
Its better to make it constexpr than const or static.