C++17中inline变量与inline static变量的区别及作用域影响
Hey there! Let’s break down the differences between inline variables and inline static variables in C++17, plus how scope impacts them—using your exact code examples as a guide.
inline and inline static Variables First, let’s cover the core rules that separate these two constructs:
1. Linkage & Instance Count
This is the biggest distinction:
- Non-static
inlinevariables: These have external linkage by default. No matter how many translation units (TUs) include the header where the variable is defined, the linker will ensure there’s exactly one single instance of the variable across your entire program. This fixes the old headache of defining global variables in headers without triggering multiple definition errors. inline staticvariables: Thestatickeyword forces internal linkage. Even withinline, each translation unit that includes this definition gets its own separate instance of the variable. Theinlinehere just lets you put the definition in multiple TUs without violating the One Definition Rule (ODR)—each TU gets its own copy.
2. Scope’s Impact
Scope affects visibility and accessibility, but it interacts with linkage in predictable ways:
- Global scope (outside any namespace):
inline T var_no_scope;is accessible anywhere in your program (viavar_no_scopeor::var_no_scope), and all code refers to the same single instance.inline static T static_var_no_scope;is only accessible within the TU where it’s used, and each TU has its own unique instance.
- Namespace scope:
namespace scope { inline T var_scope; }is accessible asscope::var_scopeacross all TUs, with one shared instance. The namespace just organizes the name—linkage behavior is identical to the global non-static inline variable.namespace scope { inline static T static_var_scope; }is accessible asscope::static_var_scope, but each TU has its own separate instance. The namespace only affects the variable’s name, not its per-TU instance behavior.
Let’s walk through each line to make this concrete:
inline T var_no_scope;
External linkage, single global instance. If you include this in 5 different.cppfiles, all of them will reference the exact samevar_no_scope. Modifying its value in one file will change it everywhere else in the program.inline static T static_var_no_scope;
Internal linkage, per-TU instance. Each.cppfile that includes this gets its ownstatic_var_no_scope. Changing its value in one TU won’t affect the variable with the same name in another TU—they’re completely separate.namespace scope { inline T var_scope; }
External linkage, namespaced shared instance. This is just a global inline variable wrapped in ascopenamespace. All TUs share the samescope::var_scope, and you can access it from anywhere by qualifying the name with the namespace.namespace scope { inline static T static_var_scope; }
Internal linkage, namespaced per-TU instance. Each TU has its ownscope::static_var_scope. The namespace keeps the name organized, but each copy is independent of others in different TUs.
- Use non-static
inlinevariables when you need a single shared instance that can safely live in a header (e.g., a global config object, a header-only singleton). - Use
inline staticvariables when you want a scoped variable that’s unique to each translation unit (e.g., a counter tracking function calls within a single TU, a local cache that doesn’t need to be shared across files).
内容的提问来源于stack exchange,提问作者Jes

