I am a bit confused by the inline variable
introduced by C++17. What are the differences between inline variable
and inline static variable
? Also will this be affected by scope?
inline T var_no_scope;
inline static T static_var_no_scope;
namespace scope {
inline T var_scope;
inline static T static_var_scope;
}
Any explanation will be appreciated!
The
inline static variable
can be defined in the class definition and may specify an initializer. It does not need an out-of-class definition:Inline variables eliminate the main obstacle to packaging C++ code as header-only libraries.
If you need to declare global variables that are shared between compilation units, declare them as
inline variables
in the header file.Any of the following names declared at namespace scope have external linkage and also names declared without a namespace for external linkage including in multiple source files must be inline.
See this Example.
This link has valuable information about inline variables.