Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of constexpr in header file

I can have a definition like this in a header file?

 constexpr double PI=3.14; 

Is there any problem in having this in a header file that would be included to several cpp files?

I am worried that since it says in standard that this constexpr has its own memory, putting it in header, and adding header to several cpp files, generate multiple copies of the same value in memory and some other nasty problems.

I am using C++11

like image 981
mans Avatar asked May 23 '18 12:05

mans


People also ask

What is constexpr used for?

constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time. A constexpr integral value can be used wherever a const integer is required, such as in template arguments and array declarations.

Should I use constexpr or const?

const applies for variables, and prevents them from being modified in your code. constexpr tells the compiler that this expression results in a compile time constant value, so it can be used in places like array lengths, assigning to const variables, etc.

When to use #define vs constexpr?

#define (also called a 'macro') is simply a text substitution that happens during preprocessor phase, before the actual compiler. And it is obviously not typed. constexpr on the other hand, happens during actual parsing. And it is indeed typed.

Does constexpr improve performance?

In Conclusion. constexpr is an effective tool for ensuring compile-time evaluation of function calls, objects and variables. Compile-time evaluation of expressions often leads to more efficient code and enables the compiler to store the result in the system's ROM.


1 Answers

constexpr implies const and const on global/namespace scope implies static (internal linkage), which means that every translation unit including this header gets its own copy of PI. The memory for that static is only going to be allocated if an address or reference to it is taken, and the address is going to be different in each translation unit.

That implied static for const variables was introduced specifically to use const instead of #define in header files in C++ to define constants. Without static there would be multiple symbol definitions linker error if that header file is included in more than one translation unit which were linked together.

In C++17 you can also make it inline, so that there is only ever a single copy of PI if an address or reference to it is taken (i.e. not static). inline variables were introduced in C++17 to allow for header-only libraries with non-const variable definitions in the header files. constexpr on static data members implies inline, so inline is unnecessary there.

In other words, you should use constexpr for your constants in header files, if possible, otherwise const. And if you require the address of that constant to be the same everywhere mark it as inline.

like image 98
Maxim Egorushkin Avatar answered Oct 03 '22 00:10

Maxim Egorushkin