In C++, does declaring a function inline make the compiler inline its calls, and what actually decides whether a call is inlined?
answer
- a preference the compiler may ignore
- one definition in many translation units
- the body must be visible at the call
- a cost model with a threshold
- ask the compiler for remarks
basics
~20 sNo. The inline specifier's binding effect is that a function may be defined in several translation units; the standard calls inline substitution only a preference. The optimiser inlines a call when it can see the body and its cost model approves.
solid answer
~50 s`inline` does two things. Its **guaranteed** effect is on the one-definition rule: an inline function may be defined, identically, in every translation unit that includes its header, and is still one entity with one address. Its effect on code generation is only a hint: the standard says inline substitution "is to be preferred" and that an implementation "is not required to perform" it. What decides inlining is the optimiser: whether the **definition is visible** in this translation unit (a function defined in another `.cpp` cannot be inlined without link-time optimisation), whether the **target is known** (a virtual call or a function pointer only after the compiler proves which function it is), and a **cost model** comparing the callee's size against a threshold. Functions without the keyword are inlined all the time, and `[[gnu::always_inline]]` or `[[gnu::noinline]]` override the heuristic. Clang's `-Rpass=inline` and `-Rpass-missed=inline` report each decision.
code
cpp · 18 lines#include <cstdio>
struct Vec3 { float x, y, z; };
// inline: may be defined in every translation unit that includes it.
// For speed it is only a hint the optimizer is free to ignore.
inline float dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
[[gnu::noinline]] float dotNoInline(Vec3 a, Vec3 b) { return dot(a, b); }
float lengthSquared(Vec3 v) { return dot(v, v); } // no inline keyword
float apply(float (*f)(Vec3, Vec3), Vec3 a, Vec3 b) { return f(a, b); }
int main(int argc, char**) {
Vec3 v{float(argc), 2, 3};
std::printf("%g %g %g\n", lengthSquared(v), dotNoInline(v, v), apply(dot, v, v));
}go deeper
Say that inline is about allowing a definition in a header and that the compiler decides inlining on its own.
List what the optimiser needs: a visible definition, a known target, a size under its threshold, and no noinline.
Verify instead of assuming: optimisation remarks, the assembly, and a measurement before reaching for always_inline.
Set the team rule: write small functions freely, keep hot code's definitions visible, and treat forced inlining as a measured exception.
## What the inline specifier means in C++ The `inline` specifier on a function is mostly about **linkage and the one-definition rule (ODR)**, not speed: - An inline function with external linkage **may be defined in more than one translation unit**, as long as every definition is identical; it is still one entity with one address, and a `static` local variable inside it is one variable. - That is why a function defined in a header must be `inline` (or a template, or `constexpr`) to avoid duplicate-symbol link errors. - Some functions are **implicitly inline**: `constexpr` functions, functions defined inside their class definition in ordinary non-module code, and deleted functions. Since C++20, a member function defined inside a class in a **named module** is no longer implicitly inline. The standard does mention speed, but as a preference: the specifier "indicates to the implementation that inline substitution of the function body at the point of call is to be preferred", and "an implementation is not required to perform this inline substitution". Compilers treat it as a mild hint at most. ## What actually decides whether a call is inlined **Inlining** means replacing a call with a copy of the callee's body. The optimiser decides per call site, and it needs all of the following: 1. **The definition is visible.** If the function is only declared in this translation unit and defined in another `.cpp`, there is no body to copy. Link-time optimisation can lift that limit; its flags belong to the compiler's own documentation. 2. **The target is known.** A direct call is easy. A call through a function pointer, a `std::function` or a virtual function can be inlined only after the optimiser proves which function it is, for example after inlining a caller that passed a known function, as the remarks below show for `apply(dot, ...)`. 3. **The cost model agrees.** Each compiler estimates the callee's size after simplification and compares it with a threshold that depends on the optimisation level and on how much has already been inlined into the caller. Small functions almost always pass; big ones usually do not, whatever their keyword. 4. **Nothing forbids it.** Direct recursion cannot be fully inlined, and a `noinline` attribute blocks it outright. Without optimisation, GCC inlines nothing except functions marked `always_inline`, which is one reason a `-O0` build of abstraction-heavy C++ is so much slower than an optimised one. ## Forcing and forbidding | Spelling | Where | Effect | |---|---|---| | `inline` | standard | ODR permission; a weak hint at most | | `[[gnu::always_inline]]` | GCC and Clang attribute | inline wherever possible, even without optimisation | | `[[gnu::noinline]]` | GCC and Clang attribute | never inline this function | | `__forceinline` | MSVC keyword | strong request to inline | These are tools for measured cases: forcing a large function inline into many call sites can grow code until instruction-cache misses cost more than the calls saved. ## Seeing the decision instead of guessing - **Optimisation remarks.** Clang's `-Rpass=inline` reports each inlined call with its cost and threshold, and `-Rpass-missed=inline` reports the refusals with a reason. - **GCC's `-Winline`** warns when a function declared `inline` could not be inlined. - **Read the assembly**: compile with `-O2 -S`, or paste the code into a compiler explorer, and look for a `call` or `bl` to the function you expected to vanish. ## Why it matters for abstraction cost Inlining is the step that makes small C++ abstractions free: once `dot(a, b)` is inlined, the optimiser sees through the function boundary and can keep values in registers, fold constants and vectorise the surrounding loop. What inlining unlocks, concretely: - **Constant propagation**: arguments that are constants at the call site become constants inside the copied body, and branches on them disappear. - **Dead-code removal**: work whose result the caller ignores is deleted. - **Register allocation across the boundary**: no argument marshalling, no spills around the call. - **Vectorisation**: a loop whose body was a call becomes a loop of arithmetic the compiler can widen. A call that is not inlined is an optimisation barrier for all of that, which is why the useful question in a performance review is "was this call inlined?", not "is this function marked `inline`?"
- Why does a function defined in a header without inline cause a link error, and why does a template not?Each translation unit that includes the header gets its own definition with external linkage, and the linker sees several definitions of one ordinary function, which the one-definition rule forbids. Declaring it `inline` makes multiple identical definitions legal. Function templates are exempt in the same way: their instantiations may appear in many translation units and the toolchain merges them.
- When could forcing a function inline make a hot loop slower?When the function is large or called from many places, copying its body everywhere grows the code; the hot loop may no longer fit the instruction cache or the decoded-instruction cache, and register pressure inside the loop rises. Forcing inline is worth doing only where a measurement shows the call itself, or a missed optimisation across it, is the cost.
- The inline keyword forces the compiler to paste the body at every call.
- A function without inline is never inlined.
- inline gives a function internal linkage, so each translation unit gets a private copy.
- Calls through a function pointer can never be inlined.
- Marking every small function inline is how you make C++ code fast.
Assumes C++23 wording; the rule that an in-class member definition is implicitly inline holds outside C++20 named modules, where it no longer applies.