[c++] Inline variables

In C++, inline variables provide a way to define variables directly in header files, allowing you to avoid linker errors caused by violating the One Definition Rule (ODR). This feature was introduced in C++17 and offers a more convenient and flexible approach to managing variables in header files.

What are Inline Variables?

An inline variable is a variable declared with the inline keyword. When used, the variable’s definition can be included in multiple translation units without violating the ODR. This means that each translation unit will have its own copy of the variable, and the linker will merge them as necessary.

Example Usage

Consider the following example of defining an inline variable in a header file:

inline int myVariable = 42;

In this case, the variable myVariable is declared and defined as inline in the header file, allowing it to be included in multiple source files without causing linker issues.

Benefits of Using Inline Variables

By using inline variables, you can avoid common issues related to the ODR, such as duplicate symbol errors during linking. Additionally, inline variables offer a more elegant solution for defining and managing global variables in header files.


Inline variables in C++ provide a practical and effective way to define variables directly in header files, making it easier to manage global variables and avoid ODR violations. By taking advantage of this feature, you can streamline your code and improve the organization of your projects.

For more information on inline variables in C++, refer to the C++ reference.