Open In App

Inline Variables in C++ 17

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An inline variable in C++ is a variable that is declared using an inline specifier. It is an exception to one definition having multiple definitions across various translation units. Inline variables have an external linkage when not declared as static.

Syntax

inline data_type variable_name = initial_value;

Note: If there are multiple definitions of inline variable, the compiler picks one of the definition when required.

How to Use Inline Variables?

It is very easy to use inline variables in C++. It is just a three-step process to declare and use inline variables in our program:

  1. First, we use the inline keyword to declare and define the variable in a header file.
  2. Now, we will include this header file and access the inline variable in the source code where we want to use the inline variable.
  3. Run the program.

Inline Variable Declaration

We can easily declare a variable as inline by using the keyword inline before the normal declaration and we can also assign value to the variable with the help of the regular assignment operator.

head.hpp

C++




inline int inline_var = 10


Accessing the Inline Variable

An inline variable defined inside a header file can be accessed by importing that header file using #include preprocessor directive.

source.cpp

C++




// C++ program to show how to access the inline variable
// with external linkage
#include "head.hpp"
#include <iostream>
using namespace std;
  
int main()
{
    // printing the value stored inside inline variable.
    cout << inline_var;
    return 0;
}


Output

10

Properties of Inline Variables

The following are some main properties of Inline Variables:

  • Unlike inline functions, inline variables can have static storage duration. If an inline variable is defined within a class scope, it behaves like a static member variable. Such member variables can be initialized inside the class. It also makes the scope of the variable static.

Example

C++




// C++ program to illustrate the inline static member
// variable
#include <iostream>
using namespace std;
  
// defining class with inline static member variable
class myClass {
public:
    inline static int var = 10;
};
  
// driver code
int main()
{
    // accessing inline static member variable
    cout << myClass::var;
    return 0;
}


Output

10
  • Inline variables have the same memory address across all translation units.
  • C++17 allows multiple definitions of inline variables in different translation units and each translation unit will have its own copy of the variable.
  • Inline variables have external linkage by default. If we want to define an inline variable with internal linkage, we can use a static specifier.

Advantages of Using Inline Variables

  • Avoiding ODR: Inline variables are exceptions to ODR, so we can safely define the same variable multiple times in different translation units.
  • Avoiding Complex Code: Inline variables simplify the code by removing the use of the workaround methods used to achieve the same functionalities.
  • Consistent and Efficient: The inline variable across multiple translation units uses the same memory address making it memory-efficient for specific tasks.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads