Module 3 · Functions and References — topic 3 of 5
Inline Functions
The inline keyword lets a function be defined in a header without linker errors and hints that calls may be expanded in place. Learn what inline really means in modern C++.
Learning objectives
- Explain the two meanings of inline
- Define small functions in headers safely
- Know that the compiler decides about actual inlining
Definition
An inline function may be defined in multiple translation units provided every definition is identical; the inline specifier also suggests (but does not force) that the compiler substitute the body at the call site.Introduction
A function call has a small cost: jumping, passing arguments, returning. For tiny functions like square(x), the compiler can avoid that cost by inserting the body directly — inlining.
Historically inline asked for this. Today optimisers inline on their own; the keyword's main job is to allow the same function definition to appear in many source files.
Why it matters
Header-only utilities (and most templates) rely on inline semantics.
Understanding inline clears up a common interview misconception that inline always makes code faster.
Detailed explanation
ODR exemption: normally a function may be defined only once in a program. An inline function may be defined in every .cpp that includes its header, as long as the definitions are identical.
Member functions defined inside a class body are implicitly inline; so are constexpr functions.
Whether a call is actually expanded is decided by the optimiser (at -O2), using size and frequency heuristics. Large or recursive functions are rarely inlined.
Since C++17, variables can also be inline, allowing a constant to be defined in a header.
Syntax
- Put inline definitions in headers.
- Don't mark large functions inline just for speed.
staticfunctions in headers also avoid ODR errors but create separate copies.
Code examples
A small inline function
100
inline variable (C++17)
236
More examples
Implicitly inline member functions
37C = 98.6F
Step-by-step explanation
- Both accessors are defined inside the class, so they are implicitly inline.
- Each call can be expanded to a single load or calculation by the optimiser.
- The class could live entirely in a header without linker errors.
Expected output
Running “Implicitly inline member functions” prints exactly:
Practical applications
- Header-only libraries (e.g. many in Boost) rely on inline functions.
- Getters and setters are usually inlined to zero cost.
- Math utility headers define inline helpers.
Best practices
- Use inline for small helpers defined in headers.
- Let the optimiser choose what to inline (compile with -O2).
- Measure before micro-optimising.
Common mistakes
Defining a non-inline function in a header
Avoid
Prefer
Two .cpp files including the header would both define twice, causing a multiple-definition linker error.
Expecting inline to speed up large code
Avoid
Prefer
Inlining large functions bloats code and can be slower; compilers ignore the hint anyway.
Important notes
- GCC and Clang offer
__attribute__((always_inline))and MSVC__forceinline, but they are rarely needed. - Debug builds (-O0) usually do not inline at all.
Key takeaways
- inline mainly permits identical definitions in many files.
- Class-body member functions and constexpr functions are implicitly inline.
- Actual inlining is the optimiser's choice.
Try it yourself
- 1Create a header with an inline
clamp(int v, int lo, int hi)and use it from two .cpp files. - 2Compile a tiny program with -O0 and -O2 and compare the assembly (
-S) for a call tosquare.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
4 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is the main practical effect of `inline` in modern C++?
- BeginnerWhich functions are implicitly inline?
- ModerateA header defines `int twice(int x) { return 2 * x; }` (no inline) and is included in two .cpp files. What happens?