In C++17, what problem do inline variables solve for globals and static data members defined in a header?
answer
- before C++17: declare here, define there
- the inline exception, now for variables
- static constexpr members became definitions
- binding a const reference is an odr-use
- constexpr global versus inline constexpr
basics
~20 sBefore C++17 a header could only declare a shared variable or static data member, and one .cpp file had to define it. An inline variable may be defined in every file that includes the header and is still one object.
solid answer
~50 sBefore C++17 a header could only **declare** a shared variable, `extern int g_frames;`, or a static data member, `static int limit;`, and exactly one `.cpp` file had to define it. C++17 extends `inline` to variables: `inline int g_frames = 0;` or `struct Limits { static inline int retries = 3; };` may sit in a header included everywhere, and the program still has **one** object with one address. A `static constexpr` data member is implicitly inline since C++17, so `static constexpr int max_retries = 3;` is now a definition. In C++14 it was only a declaration, and an odr-use such as `std::max(Limits::max_retries, 1)`, which binds a `const int&`, failed to link without an out-of-class definition, now redundant and deprecated. A namespace-scope `constexpr int k = 7;` is still internal, one copy per file; `inline constexpr` makes it one object.
code
console · 8 lines$ clang++ -std=c++14 -O0 a.cpp main.cpp -o p14
Undefined symbols for architecture arm64:
"Limits::max_retries", referenced from:
_main in main-d8376c.o
ld: symbol(s) not found for architecture arm64
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
$ clang++ -std=c++17 -Wall -Wextra a.cpp main.cpp -o p17 && ./p17
3 0 1go deeper
Recall that since C++17 inline int x = 0; or static inline int n = 3; in a header defines one shared variable, with no separate .cpp definition needed.
Explain the odr-use story: why C++14 static constexpr members needed an out-of-class definition when bound to a reference, and why C++17 made them implicitly inline.
Know which constants are per-file copies and which are single objects, and catch the cases where it matters: large tables, address comparisons and initializers that differ between builds.
Decide the house style for constants and globals in headers on C++17 and later, and plan removing the now-deprecated out-of-class definitions when the codebase's baseline moves.
## The problem before C++17 The one-definition rule allows a **non-inline** variable only one definition in the whole program. A header that is included by many `.cpp` files therefore could not define one; it had to split the work: ```cpp // limits.h, C++14 style struct Limits { static int retries; // declaration only static constexpr int max_retries = 3; // C++14: still a declaration }; extern int g_frames; // declaration only // limits.cpp, the one definition of each int Limits::retries = 3; constexpr int Limits::max_retries; // needed if it is odr-used int g_frames = 0; ``` Header-only libraries could not have a mutable global or a static data member at all without tricks, such as a function returning a reference to a function-local static. Class templates were the exception: a template's static data member could already be defined in the header, because templates have their own allowance to be defined in several translation units. ## What an inline variable is C++17 lets `inline` apply to variables, with the same meaning it has for functions. An **inline variable** with external linkage may be defined in every translation unit that includes it, provided every definition is identical, and the standard states that it is still **one entity with one address**: ```cpp // limits.h, C++17 inline int g_frames = 0; // one object for the program struct Limits { static inline int retries = 3; // defined in the class static constexpr int max_retries = 3; // implicitly inline: a definition }; ``` No `.cpp` file is needed. The standard's own rule is that a function or static data member declared `constexpr` on its first declaration is **implicitly inline**. ## The static constexpr member and odr-use The trap that made this famous is the odr-use. A value that is only read as a constant, as in `int a[Limits::max_retries];`, never needs storage. Binding it to a reference does, and `std::max` takes its arguments as `const T&`. On this machine, with Apple clang 21 at `-O0`: - with `-std=c++14`, `std::max(Limits::max_retries, 1)` failed to link: `Undefined symbols for architecture arm64:` naming `Limits::max_retries`; - with `-std=c++17`, the same code linked and ran. An optimised C++14 build can happen to link, because the reference is optimised away; that is why the bug often appeared only in debug builds. The old out-of-class line `constexpr int Limits::max_retries;` is now a redundant redeclaration, and the standard lists that usage as **deprecated**. ## Namespace-scope constants: constexpr versus inline constexpr | In a header, at namespace scope | Linkage | Objects in the program | |---|---|---| | `const int k = 7;` | internal | one per including file | | `constexpr int k = 7;` | internal, since `constexpr` implies `const` | one per including file | | `inline constexpr int k = 7;` | external | **one** | | `extern const int k;` + one definition | external | one | For a namespace-scope variable, unlike a static data member, `constexpr` does **not** imply `inline`. The per-file copies are usually harmless for an `int`, but they matter for a large table, and whenever code compares addresses: in a test on this machine, `&k_const` taken in two files compared unequal, while `&k_inline` compared equal. ## When to use which - **Constants in headers:** `inline constexpr` for anything non-trivial, so there is one object; plain `constexpr` is fine for small scalars. - **Static data members:** `static inline` for mutable ones, `static constexpr` for constants, with no out-of-class definitions in new code. - **Mutable globals:** `inline` removes the boilerplate, not the design problem. Each one is still shared state, and dynamic initialization of variables in different translation units still has ordering rules of its own. - **Migrating to a C++17 baseline:** out-of-class `constexpr` member definitions can be deleted, and each pair of an `extern const` declaration in a header plus a definition in a `.cpp` file can collapse into one `inline constexpr` line, when the constant is a constant expression. - **Every definition must be identical.** As with inline functions, a macro or a build flag that changes an inline variable's initializer in one file is an ODR violation that nothing is required to diagnose.
- Does constexpr imply inline for every variable?No. A `constexpr` static data member is implicitly inline since C++17, but a namespace-scope `constexpr` variable is not: it is `const`, so it has internal linkage and each file gets its own copy. Writing `inline constexpr` at namespace scope gives one object with external linkage.
- What does an inline variable require of its definitions?The same as an inline function: every translation unit's definition must be the same token sequence with the same meaning. If a macro or build flag changes the initializer in one file, the program violates the one-definition rule and no diagnostic is required, so one file may silently observe a different value.
- An inline variable gives each translation unit its own copy.
- A namespace-scope constexpr variable is automatically inline.
- static constexpr members still need an out-of-class definition in C++17.
- inline on a variable means its value is substituted and it has no storage.
- C++14 static constexpr members failed only in release builds.
Assumes C++17 or later; inline variables and implicitly inline static constexpr data members arrived in C++17, and the C++14 behaviour is described as history.