C++ 17 – Inline Variables
What is the approach we would take when a variable should be declared in one header or source file but should be accessible across different files? The approach is to declare the variable in one of the files and declare it as an “extern” everywhere else we use it.
How do we define a static member variable of a class? We would declare it inside the class and define it outside the class. We will assign or initialize a value while defining it.
Both these things get simplified from C++ 17 using inline variables. We need to use the inline keyword on the variables.
We do not need to define them everywhere else as “extern” when we use the keyword on global variables. In the case of static member variables, we do not need to define them outside of class.
Let us see this step-by-step using a sample C++ program. It is written in two parts – one header file and one cpp file.
You can download the program files at https://github.com/codeversionmaster/cplusplus/blob/cplusplus17/inline_variables.h and https://github.com/codeversionmaster/cplusplus/blob/cplusplus17/inline_variables.cpp.
There are two files – inline_variables.h and inline_variables.cpp.
$ ls inline_variables.*
inline_variables.cpp inline_variables.h
inline is used in two ways.
inline int global_variable would make the variable available in all the source files with the same address without defining the extern keyword.
static inline int static_member_variable makes the member variable available as a static member variable without any need for redefining it. The initialization to value 10 is done within the class itself.
$ cat inline_variables.h
// inline_variables.h
#pragma once
inline int global_variable = 42;
class IVClass {
public:
static inline int static_member_variable = 10;
};
Here, we use the global and static member variables like we usually do in any C++ program. We will try to print the variables.
$ cat inline_variables.cpp
// inline_variables.cpp
#include <iostream>
#include "inline_variables.h"
using namespace std;
int main() {
cout << "Static member variable is: " << IVClass::static_member_variable << endl;
cout << "Global variable is: " << global_variable << endl;
return 0;
}
You can compile and run the program as shown. The global and static member variables are accessible at the same address and value as in the header file.
$ g++ -std=c++17 inline_variables.cpp -I. -o inline_variables
$ ./inline_variables
Static member variable is: 10
Global variable is: 42
We can try compilation using -std=c++14 to see that compiler throws an error. Based on the errors observed, we can conclude that the inline variable is a concept introduced in C++ 17 onwards.
$ g++ -std=c++14 inline_variables.cpp -I. -o inline_variables
In file included from inline_variables.cpp:3:
inline_variables.h:4:1: warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
4 | inline int global_variable = 42;
| ^~~~~~
inline_variables.h:8:12: warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
8 | static inline int static_member_variable = 10;