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 function unless it is attached to a named module(since C++20).
A function declared constexpr or consteval(since C++20) on its first declaration is implicitly an inline function. 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) |
Contents |
[edit] Explanation
An inline function or inline variable(since C++17) has the following properties:
- The definition of an inline function or variable(since C++17) must be reachable in the translation unit where it is accessed (not necessarily before the point of access).
- An inline function or variable(since C++17) with external linkage (e.g. not declared static) has the following additional properties:
- 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 functions and variables(since C++17)) all definitions are identical. For example, an inline function or an inline variable(since C++17) may be defined in a header file that is included in multiple source files.
- 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 is preferred over function call, that is, instead of executing the function call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids overhead created by the function call (passing the arguments and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times.
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. Those optimization choices do not change the rules regarding multiple definitions and shared statics listed above.
Because the meaning of the keyword inline for functions came to mean "multiple definitions are permitted" rather than "inlining is preferred" since C++98, that meaning was extended to variables. |
(since C++17) |