The inline keyword in C++ does two jobs. It is a hint that the compiler may replace a function call with the function’s body. It also lets a function be defined in more than one source file, as long as every definition is identical. The second job matters more in everyday code, because it is why inline functions show up in header files.
What the inline keyword means
Here is a small function marked inline:
inline int square(int x) {
return x * x;
}
The keyword suggests that calls to square can be expanded in place, which can save the cost of a function call. The compiler makes the final decision. At low optimization levels it may still emit a normal call, and at higher levels it may expand functions that have no inline keyword at all. Treat inline as a hint, not a command.
Why inline functions belong in header files
The One Definition Rule (ODR) says that a regular, non-inline function or variable can have only one definition in a program. Suppose you put a regular function in a header, and two .cpp files include that header. Each file compiles its own copy of the function, and the linker reports a multiple definition error.
Marking the function inline removes that error. Each file may hold its own copy, as long as all the copies are identical. Here is a complete example.
math_utils.h:
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
inline int square(int x) {
return x * x;
}
#endif
The #ifndef lines form a header guard. They keep the header from being processed twice in the same file.
main.cpp:
#include <iostream>
#include "math_utils.h"
int main() {
std::cout << square(4) << '\n';
}
other.cpp:
#include "math_utils.h"
int twice_square(int x) {
return 2 * square(x);
}
Build both files together with g++ -std=c++17 main.cpp other.cpp -o demo. The build links without errors, because square is inline. If you remove the inline keyword, the same build fails at the link step.
Every definition of an inline function must be the same. If two files give the same inline function different bodies, the program has undefined behavior, and compilers often do not report it.
Member functions and templates
A function defined inside a class body is implicitly inline, so you do not need to write the keyword:
class Counter {
public:
void increment() { ++count_; }
int value() const { return count_; }
private:
int count_ = 0;
};
Because of this, a class defined in a header can be included in several files. Function templates can also live in headers without the keyword.
Inline variables in C++17
C++17 extended the idea to variables. Without inline, each file that includes a header gets its own copy of a namespace-scope constexpr variable. With inline, every file refers to one shared object:
// config.h
#ifndef CONFIG_H
#define CONFIG_H
inline constexpr int kMaxUsers = 100;
#endif
Inline does not force inlining
The compiler decides whether to expand a call. It considers the size of the function, the optimization level, and other factors. Some compilers offer non-standard attributes that request expansion, such as __attribute__((always_inline)) in GCC and Clang, and __forceinline in MSVC. These are rarely needed.
Inlining can also make a program larger. A larger program can run slower, because its code takes up more space in the processor’s cache. Keep inline functions short.
Common mistakes
- Putting a regular, non-inline function definition in a header that several
.cppfiles include. The fix is to addinline, or to move the definition into one.cppfile. - Assuming
inlinemakes a function faster. It may or may not. - Writing two different definitions of the same inline function. That is undefined behavior.
When to use inline
- Short functions that belong in a header, such as small helpers or accessors.
- Namespace-scope constants that several files share, in C++17 and later.
- Functions defined inside a class body, which are already inline.
For larger functions, put the definition in a .cpp file. A change to the function body then requires recompiling only that one file.
Inline in C
C99 also added the inline keyword, but its rules differ from the C++ rules. If you are writing C, check the C rules before you copy a C++ pattern.