CKTIS / C++
Inline variable: one object across translation units
Define an inline variable with external linkage in a shared header to give multiple translation units the same object. Compare it with an ordinary namespace-scope `const` that gives each translation unit its own object.
Version: ISO C++23; implementation support may vary
Syntax
namespace config {
inline const int shared_limit = 42;
const int local_limit = 42;
}Since C++17, the inline specifier can declare an inline variable. It does not promise code expansion or faster execution. A translation unit is typically a .cpp file with its included header text after preprocessing. External linkage lets declarations in different translation units name the same variable.
The One Definition Rule (ODR) forbids defining the same variable twice in one translation unit. In ordinary headers outside named modules, an inline variable with external linkage can nevertheless have matching definitions in different translation units: they denote one object with one address. Its definition must be reachable from the end of each translation unit that odr-uses it outside a discarded statement. Taking its address with & in a potentially evaluated expression is an odr-use.
Matching requires, among other things, the same token sequence after preprocessing. Corresponding names must generally resolve to the same entities as well. If a macro changes the initializer in just one translation unit so that the definition has different tokens, the program is ill-formed and no diagnostic is required.
In the header shown here, inline const int shared_limit has external linkage, while the ordinary namespace-scope const int local_limit has internal linkage: each translation unit that includes the header gets its own local_limit. Both variables have static storage duration. inline does not force external linkage: static inline at namespace scope has internal linkage.
Examples
// limits.hpp
#ifndef CKTIS_LIMITS_HPP_INCLUDED
#define CKTIS_LIMITS_HPP_INCLUDED
namespace config {
inline const int shared_limit = 42;
const int local_limit = 42;
}
#endif
// other.cpp
#include "limits.hpp"
const int* shared_from_other() {
return &config::shared_limit;
}
const int* local_from_other() {
return &config::local_limit;
}
// main.cpp
#include "limits.hpp"
#include <iostream>
const int* shared_from_other();
const int* local_from_other();
int main() {
std::cout << std::boolalpha
<< "shared: " << (&config::shared_limit == shared_from_other()) << '\n'
<< "local: " << (&config::local_limit == local_from_other()) << '\n';
}
Result
shared: true local: false
Both `.cpp` files include the same header. The `==` operator compares pointers, not the values `42`: it yields `true` for `shared_limit` and `false` for the two distinct `local_limit` objects.
Related lessons
- C++ · Lesson 7 · Advanced · Version: 3
Sources
- WG21 N4950 — C++23 working draft: [lex.phases], [basic.def.odr], [basic.link], [basic.stc.static], [dcl.inline]
- The inline specifier [dcl.inline] — C++ working draft
- One-definition rule [basic.def.odr] — C++ working draft
- Program and linkage [basic.link] — C++ working draft
- WG21 P0636R0 — Changes between C++14 and C++17