Reputation: 2684
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!
Upvotes: 16
Views: 17617
Reputation: 11
Great answer about inline variables and why we want to use them can be found here. In short inline variable allows to have multiple definition of a variable (even across multiple files), that will result in one variable in memory. This allow constexpr global variables in header files.
header.h
namespace constants
{
inline constexpr double P = 1.11;
}
Behavior is undefined when definitions have different values, but this shouldn't be a problem when multiplication of definitions occur only due to header file.
Others have pointed to a nice application in classes:
template<typename T>
struct C
{
static inline constexpr int c = 10;
};
And then you can reference this variable anywhere, e.g. with:
C<int>::c;
Upvotes: 1
Reputation: 21
definition from c++ reference: citation: https://en.cppreference.com/w/cpp/language/inline
inline variable is defined as inline static keyword. Hence inline static vartype yourvariablename // is a inline variable.
Interesting note about inline "A function defined entirely inside a class/struct/union definition, whether it's a member function or a non-member friend function, is implicitly an inline function if it is attached to the global module"(C++ 20)
Ok from personal experience here's why inline static is a really really big thing:
If all you need todo is initialize a static variable in a C++ file then you can remove the C++ file and use inline static initialization(but your code will require C++ 17 and up)
Initializing static variables in pre C++ 17 classname templates is just overly verbose. You would have to do something like:
template
class classname
{
static yourtype yourstaticvariable = yourvalue; // and you'll have to initialize it
};
template yourtype classname::yourstaticvariable = yourvalue;
OR with inline static you can just do:
template <class T>
class classname
{
static yourtype yourstaticvariable = yourvalue; // initialized
}
But there some caveats with inline static:
But overall it's a fantastic feature. When in doubt initialize with inline static and break the I need a c++ file to initialize a static variable pattern.
Upvotes: 0
Reputation: 63775
inline
is applicable to variables only with static storage duration.
All of the variables in your example have namespace scope, giving them static storage duration. Declaring them inline
has no net effect if they are static
.
A variable inside a class
, struct
or union
only has static storage duration if it is declared static
. Those varibles must be static
if they are to be inline
.
Upvotes: 1
Reputation: 96236
At namespace scope:
inline static
seems to be equivalent to just static
.
inline
on variables only has effect when you define the same variable in several translation units. Since static
restricts the variable to a single TU, you can't have more than one definition.
At class scope:
inline
can only appear on static
variables.
It has its normal effect, allowing you to initialize the variable directly in the header. Either:
struct A
{
inline static int a = 42;
};
Or:
struct A
{
static int a;
};
inline int A::a = 42;
At function scope:
inline
is not allowed.
Upvotes: 11
Reputation: 347
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:
struct X
{
inline static int n = 1;
};
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.
Also will this be affected by scope?
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.
Upvotes: 0
Reputation: 2334
For me it becomes more interesting when it is a data members. In C++17
you can declare your static data members as inline
. The advantage is that you don't have to allocate space for them in a source file
. For example:
class A
{
// Omitted for brevity
static inline int b = 0;
};
So int A::b;
can be removed from the source file.
Upvotes: 7