Consider code like the following, where the literal some_magic_int
(e.g. 3) is given a name just to make a bit clearer what constant it represents:
void f() {
static constexpr int this_variable{some_magic_int};
do_something_with(this_variable);
}
int main() {
// ...
f();
// ...
}
I'm pretty sure constexpr
has to be here: some_magic_int
is literal, so it never changes, and I'm giving it a name just for clarity, not to give a mean to change it, so it should be at least const
; then why not constexpr
to have it at compile-time?
But what about static
? Is it just unnecessary? Or is it detrimental? If so, why? And also, does it have any observable effect, when paired with constexpr
in the declaration of a local variable?
As regards the question to which this is marked as duplicate of, it is about static constexpr int x [] = {}
, and not static constexpr int x {}
. This highlights at least one difference between that case (attributes applying to x
pointer vs attributes applied to *x
pointee) and my case (there's no pointer).
Furthermore, once I add constexpr
to the specifier of a local variable (where it makes sense, e.g. to a int
), I'm saying that variable is compile-time known. Why in the world doesn't that imply that no run-time entity is needed whatsoever?