`inline` — C++ Keyword

`inline` — C++ Keyword

The inline keyword in C++: hints for inlining and allows multiple definitions across translation units.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

inline

Has two distinct effects: (1) it is a hint to the compiler to expand the function at call sites (though modern compilers ignore or override this freely), and (2) it allows a function or variable to be defined in multiple translation units without violating the ODR (One Definition Rule).

Syntax

inline return-type function-name(params) { body }
inline Type variable-name = value;   // C++17 inline variable

Example

#include <print>

// Defined in a header – inline prevents ODR violations
inline int clamp(int val, int lo, int hi) {
    return val < lo ? lo : val > hi ? hi : val;
}

// Inline variable (C++17) – safe to define in a header
inline constexpr double kPi = 3.14159265358979;

int main() {
    std::println("{}", clamp(15, 0, 10));   // 10
    std::println("{:.6f}", kPi);
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}