Skip to content

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++.

Beginner 10 min read·Lesson 10 of 40 in C++ Programming

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

C++
// math_utils.hpp
inline int square(int x) { return x * x; }
inline constexpr double kPi = 3.141592653589793;
class Point {
int x_;
public:
int x() const { return x_; } // implicitly inline
};
  • Put inline definitions in headers.
  • Don't mark large functions inline just for speed.
  • static functions in headers also avoid ODR errors but create separate copies.

Code examples

A small inline function

C++
#include <iostream>
inline int cube(int x) { return x * x * x; }
int main() {
int total = 0;
for (int i = 1; i <= 4; ++i) total += cube(i);
std::cout << total << '\n';
return 0;
}
Output
100

inline variable (C++17)

C++
#include <iostream>
inline constexpr double kGst = 0.18;
inline double with_gst(double amount) { return amount * (1 + kGst); }
int main() {
std::cout << with_gst(200.0) << '\n';
return 0;
}
Output
236

More examples

Implicitly inline member functions

C++
#include <iostream>
class Temperature {
double celsius_;
public:
explicit Temperature(double c) : celsius_(c) {}
double celsius() const { return celsius_; }
double fahrenheit() const { return celsius_ * 9 / 5 + 32; }
};
int main() {
Temperature t{37.0};
std::cout << t.celsius() << "C = " << t.fahrenheit() << "F\n";
return 0;
}
Output
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:

TextOutput
37C = 98.6F

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

// util.hpp
int twice(int x) { return 2 * x; }

Prefer

// util.hpp
inline int twice(int x) { return 2 * x; }

Two .cpp files including the header would both define twice, causing a multiple-definition linker error.

Expecting inline to speed up large code

Avoid

inline void process_everything(/* 200 lines */);

Prefer

void process_everything(/* 200 lines */); // let the optimiser decide

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

  1. 1
    Create a header with an inline clamp(int v, int lo, int hi) and use it from two .cpp files.
  2. 2
    Compile a tiny program with -O0 and -O2 and compare the assembly (-S) for a call to square.

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?
Practise this topic