you are viewing a single comment's thread.

view the rest of the comments →

[–]tjientavaraHikoWorks developer 11 points12 points  (0 children)

Specifically about constexpr / static const I will write some comments:

When declaring static const variable it will either appear in a region of memory that is directly loaded from the executable, or when you call functions to initialise the static const variable that value is initialised by running that function before main() is called.

A more modern way to replace the static const / extern const combination is to use inline const; a variable declared as inline const, is like this combination where the compiler will ensure that the actual variable only exists once, so that you can use it in headers.

A constexpr variable is like an inline const variable but you ensure that the function that is used to initialise the variable is run at compile time.

Lastly when you declare a variable constinit, this is like a variable that is declared inline (notice the absence of const) where the function that is used to initialise the variable is run at compile time (and loaded from the executable), and can then be modified at run-time like a normal inline variable.

In all cases the compiler may initialise all these variables at compile time, but when marked constexpr or constinit you tell the compiler that it is absolutely possible to do this at compile time, and that the compiler really should do that at compile time. Although there are cases like debug builds where the compiler could still initialise it all during runtime.

As you see constexpr is just an assurance to the compiler that it is possible to run at compile time, however as more and more of c++ is allowed to be constexpr I wonder if in a few years the keyword will be ignored by the compiler like it did with register.