constexpr
sometimes implies const
Should one - in those cases - always prefer constexpr
over const
due to for example run time speed-ups or are there any down sides?
constexpr
sometimes implies const
Should one - in those cases - always prefer constexpr
over const
due to for example run time speed-ups or are there any down sides?
In short, yes, you should prefer constexpr as long as your expression can be and should be evaluated at compile time.
See this question for more detail: Difference between `constexpr` and `const`
constexpr
means that the compiler knows it is constant and can compute its value. This allows you to use it in compile-time constructs such as templates, and potentially allows the compiler to do exact optimizations.
If it can be constexpr
, let it be.