3

When to use constexpr and when to use extern const?

I have a situation like:

  • in header (.h):

    extern const int MAX_NUMBER_OF_ROWS; 
  • in source (.cpp):

    const int MAX_NUMBER_OF_ROWS= 99; 

The files (header and source) contains just such definitions and declarations.

Is it recommanded to use just the constexpr in the header file and get rid of the source file, like in here?:

// this is in the header file. There is no cpp file any more. constexpr int MAX_NUMBER_OF_ROWS= 99; 
2
  • 8
    these are orthogonal - you could always have used const int MAX_NUMBER_OF_ROWS = 99; in the header; and then the question would be whether to change const to constexpr. Having it the way you have it is usually only done if the value is not known at the time of the header being included Commented Aug 11, 2016 at 7:50
  • OK, and if it is in the header as you said, can it be constexpr, right? Commented Aug 11, 2016 at 7:52

3 Answers 3

15

Using extern const in the header file only tells the compiler that the variable exists and that it is not modifiable. It doesn't tell the compiler its value which means it's not a compile-time constant anymore. If it's not a compile-time constant then it can't be used for e.g. case or as an array size.

As said by M.M in the comment, either use

const int MAX_NUMBER_OF_ROWS= 99; 

or

constexpr int MAX_NUMBER_OF_ROWS= 99; 

directly in the header file and it will be a compile-time constant in all translation units that include the header file.

Sign up to request clarification or add additional context in comments.

11 Comments

The question is now: when to use const int and when constexpr int in the header file?
In C you get linking errors, if you do plain const int MAX_NUMBER_OF_ROWS= 99; in header and include it in multiple source files. Is this different in C++, or is this missing from your answer?
@user694733 yes it is different in C++; const variables have internal linkage unless declared with extern (in the definition, or in a prior declaration of the same variable)
@mtb That's more a question of personal preference than anything else really. The only time it is not, is if you're targeting older compilers that doesn't support C++11.
In C++>=17 constexpr makes a variable constexpr inline, getting rid of ODR issues.
|
4

extern const can be used if you plan to initialize the variable to a different value in the future, and don't want codes that use this variable to be recompiled. (I've never seen this need, but it might be useful in certain cases.) As others have said, the value of this variable cannot be used in constant expressions.

constexpr can be used when the value of the variable is know at compile-time.

1 Comment

a good example is version strings. if u use cmake to write current git revision into the program, it will change all the time so dont want in header
3

constexpr is better if your compiler supports it.

It evaluate the value of the function or variable at compile time, with better performance (less memory seek and read) and less memory costing (not exist in data section).

However some compilers don't support it, for example, visual studio 2013 or older.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.