inline Functions in C++
inline suggests the compiler paste a small function's body directly at call sites instead of paying call overhead — historically a manual optimization hint, today mostly a linkage tool: inline functions may be defined identically in multiple translation units (headers!) without multiple-definition linker errors.
Modern compilers inline aggressively on their own using cost models far smarter than human hints, so write inline for header-defined helpers and tiny accessors, and let -O2 decide the rest. Member functions defined inside a class body are implicitly inline for the same reason.
A hint, not a command
The compiler may inline any suitable function and ignore inline on huge ones — it optimizes by measured cost, not by keyword.
Headers need it
A non-inline function defined in a header included by two .cpp files triggers multiple-definition link errors; inline (and templates, and constexpr) are exempt.
Class bodies imply it
Member functions written inside the class definition are implicitly inline — another reason tiny getters live in the class body.
Measure, don't sprinkle
Inlining trades binary size for speed; profile-guided and link-time optimization (-flto) beat manual inline placement.
#include <iostream> inline int square(int x) { return x * x; } int main() { std::cout << square(6) << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.