constexpr or const static

Re: constexpr or const static

by Ronan Boulic -
Number of replies: 0

If the value is used only within a specific module the static constexpr is what you look for because it makes it invisible in other modules.

I mentioned in a previous course that we must not put a static variable X declaration in a header file.h  because that constant variable X will be re-declared locally in each module where this header file is included. NOW in the context of your question, it means that the constant variable X will be duplicated in each of these modules. It might not be such a problem after all because it is a constant and memory space is not an issue on regular computers nowadays.

if we want to prevent such a duplication and if the value is needed in other modules, I tend to prefer symbols created with define or enum in the header file.h like with error.h and define.h.

Why define or enum in that larger context ? => The problem with multi-modules constexpr or const variables is that we have the issue of handling a kind of  "constant global variable" across modules ; there are additional rules for such declaration with one additional extern keywords = it's overly complicated for little benefit. I don't recommend it.