C++23 says that constexpr functions and static data members are implicitly inline, but it does not say the same for namespace-scope constexpr variables. It also says that: "For any definable item D with definitions in multiple translation units, if D is a non-inline non-templated function or variable... the program is ill-formed."
Since inline variables were only introduced in C++17, there are a lot of legacy header files with non-inline constexpr variables. Why is it not illegal for multiple translation units to contain definitions of the same constexpr variable, as in:
// include.h
constexpr int my_constant = 1;
Your example does not define the same variable more than once; it defines a separate variable with internal linkage in each TU where include.h
is #include
d ([basic.link]/8).
A
constexpr
specifier used in an object declaration declares the object as const.
The name of an entity that belongs to a namespace scope has internal linkage if it is the name of
- a non-template variable of non-volatile const-qualified type