inline
specifier
The inline specifier, when used in a function's decl-specifier-seq, declares the function to be an inline function
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 functionunless it is attached to a named module(since C++20)
A function declared constexpr or consteval(since C++20) A deleted function is implicitly an inline function: its (deleted) definition can appear in more than one translation unit. |
(since C++11) |
The inline specifier, when used in a decl-specifier-seq of a variable with static storage duration (static class member or namespace-scope variable), declares the variable to be an inline variable A static data member declared constexpr on its first declaration is implicitly an inline variable. |
(since C++17) |
Explanation
An inline functionor inline variable(since C++17) has the following properties:
- The definition of an inline function or variable (since C++17)
- An inline functionor variable(since C++17) with external linkage (e.g. not declared static
-
- There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit and (for non-static inline functionsand variables(since C++17)) all definitions are identical. For example, an inline functionor an inline variable(since C++17)
- It must be declared inline in every translation unit.
- It has the same address in every translation unit.
In an inline function,
- Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit).
- Types defined in all function definitions are also the same in all translation units.
Inline const variables at namespace scope have external linkage by default (unlike the non-inline non-volatile const-qualified variables). |
(since C++17) |
The original intent of the inline keyword was to serve as an indicator to the optimizer that inline substitution of a function
Since inline substitution is unobservable in the standard semantics, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline
Because the meaning of the keyword inline |
(since C++17) |
Notes
If an inline function or variable (since C++17)
The inline specifier cannot be used with a functionor variable(since C++17)
The inline specifier cannot re-declare a functionor variable(since C++17)
The implicitly-generated member functions and any member function declared as defaulted on its first declaration are inline just like any other function defined inside a class definition.
If an inline function is declared in different translation units, the accumulated sets of default arguments must be the same at the end of each translation unit.
In C, inline functions do not have to be declared inline in every translation unit (at most one may be non-inline or extern inline
See static data members for additional rules about inline static members. Inline variables eliminate the main obstacle to packaging C++ code as header-only libraries. |
(since C++17) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_inline_variables |
201606L |
(C++17) | Inline variables |
Keywords
Example
Header "example.h":
#ifndef EXAMPLE_H #define EXAMPLE_H #include <atomic> // function included in multiple source files must be inline inline int sum(int a, int b) { return a + b; } // variable with external linkage included in multiple source files must be inline inline std::atomic<int> counter(0); #endif
Source file #1:
#include "example.h" int a() { ++counter; return sum(1, 2); }
Source file #2:
#include "example.h" int b() { ++counter; return sum(3, 4); }
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 281 | C++98 | a friend function declaration could use the inline specifier even if the friended function is not an inline function |
prohibit such uses |
CWG 317 | C++98 | a function could be declared inline even if it has a non-inline definition in the same translation unit before the declaration |
the program is ill- formed in this case |
CWG 765 | C++98 | a type defined in an inline function might be different in different translation units |
such types are the same in all translation units |
CWG 1823 | C++98 | string literals in all definitions of an inline function were shared across all translation units |
the requirement is removed due to consistency and implementations |
CWG 2531 | C++17 | a static data member could be implicitly inline even if it was not declared constexpr on its first declaration |
it is not implicitly inline in this case |
See also
C documentation for inline
|