`inline` — C++ Keyword
`inline` — C++ Keyword
The inline keyword in C++: hints for inlining and allows multiple definitions across translation units.
`inline` — C++ Keyword
The inline keyword in C++: hints for inlining and allows multiple definitions across translation units.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
inlineHas 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).
inline return-type function-name(params) { body }
inline Type variable-name = value; // C++17 inline variable
#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);
}
inline function must have the same definition (usually provided via a header).inline is required for function templates and constexpr functions defined in headers (they are implicitly inline).[[gnu::always_inline]] / [[msvc::forceinline]] attributes forcibly inline a function.inlineint 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;
}