C++ Reference ยท Keywords and Statements
The inline specifier
Reference for C++ inline: what it really means (ODR, not speed), inline functions in headers, inline variables from C++17 and inline namespaces.
Despite its name, inline is not mainly a performance hint. Its binding meaning is about the One Definition Rule: an inline function or variable may be defined in several translation units, as long as every definition is identical, and the linker merges them into one. That is what makes it legal to put function bodies in headers.
Compilers decide for themselves whether to expand a call in place; they inline functions that are not marked inline and ignore the keyword on large ones. Treat inline as "this definition may appear in many files".
inline int square(int x) { return x * x; } // safe in a header inline constexpr double tau = 6.283185307; // inline variable (C++17) inline namespace v2 { struct Api {}; } // inline namespace
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
inline return-type function(params) { body } inline type variable = value; // C++17 inline namespace name { ... } // C++11
| Applied to | Effect |
|---|---|
| Function | May be defined in more than one translation unit; every unit that calls it must see the definition. Hints that inlining is desirable. |
| Variable | One shared object even though the definition appears in many units (C++17). |
| Namespace | Its members behave as if declared in the enclosing namespace, which allows library versioning. |
Usage notes
- Implicitly inline: functions defined inside a class body,
constexprandconstevalfunctions, functions defined with= defaultin-class, andstatic constexprdata members (since C++17). - All definitions of an inline function must be token-for-token identical; otherwise the program is ill-formed with no diagnostic. A header included everywhere satisfies this naturally.
- A non-inline function defined in a header and included by two
.cppfiles gives a "multiple definition" linker error. Making itinlinesolves it. - A function that is inline in one unit must be declared
inlinebefore its first use in every unit where it is used and defined. - Before C++17, constants in headers were declared
static const(a separate copy per file).inline constexprgives a single object whose address is the same everywhere. - Inlining is an optimization the compiler performs on its own with
-O1and above. Use[[gnu::always_inline]]or__forceinlinefor compiler-specific control, rarely. - Excessive inlining can enlarge the binary and hurt instruction-cache use.
std::uses inline namespaces internally (for examplestd::literals::string_literals,std::__cxx11), so you canusing namespace std::literals;to get every literal operator at once.