What does __inline__ mean ?
Categories:
Understanding the 'inline' Keyword in C and C++

Explore the purpose, implications, and common misconceptions surrounding the inline
keyword in C and C++ programming.
The inline
keyword is a fascinating and often misunderstood feature in C and C++. While its primary intent is to suggest to the compiler that a function should be inlined (meaning its body is inserted directly at the call site instead of a traditional function call), its actual behavior is more nuanced. This article delves into what inline
truly means, its benefits, drawbacks, and how modern compilers typically handle it.
The Compiler's Suggestion, Not a Command
At its core, inline
is a hint to the compiler. It's not a directive that forces the compiler to inline a function. The compiler is free to ignore this hint if it determines that inlining would not be beneficial, or if the function is too complex, recursive, or has other characteristics that make inlining impractical or detrimental to performance. Conversely, a compiler might choose to inline a function even if it's not marked inline
, especially with optimization flags enabled.
flowchart TD A[Function Call] --> B{Is function marked 'inline'?} B -->|Yes| C{Compiler considers inlining} B -->|No| D{Compiler considers inlining (optimization)} C --> E{Function suitable for inlining?} D --> E E -->|Yes| F[Function body inserted at call site] E -->|No| G[Traditional function call (jump/return)] F --> H[Reduced overhead, potentially larger code] G --> I[Increased overhead, smaller code] H & I --> J[Optimized Executable]
Compiler's Decision Process for Inlining
Benefits of Inlining
When a function is inlined, the overhead associated with a traditional function call (pushing arguments onto the stack, saving registers, jumping to the function's address, returning, etc.) is eliminated. This can lead to performance improvements, especially for small, frequently called functions. Additionally, inlining can expose more optimization opportunities to the compiler, as the context of the call site becomes available within the inlined code.
/* Example of a small, frequently called function */
inline int add(int a, int b) {
return a + b;
}
int main() {
int x = 5;
int y = 10;
int sum = add(x, y); // Compiler might inline 'add' here
return 0;
}
A simple inline
function in C.
Drawbacks and Considerations
While inlining can improve performance, it's not a silver bullet. Overuse of inline
can lead to 'code bloat,' where the executable size increases significantly because the function's code is duplicated at every call site. This can, in turn, lead to worse cache performance and potentially slower execution, negating the benefits of reduced call overhead. It's a trade-off between code size and execution speed.
-O2
or -O3
flags) is more effective than manually marking every small function as inline
. The compiler often makes better decisions about when to inline than a human programmer.Linkage and One Definition Rule (ODR)
In C++, the inline
keyword also plays a crucial role in managing the One Definition Rule (ODR). If a function is defined in a header file and included in multiple translation units, it would normally violate the ODR, leading to linker errors (multiple definitions of the same function). Marking a function inline
allows it to be defined in multiple translation units without violating the ODR, as long as all definitions are identical. The linker is then responsible for ensuring only one instance of the function exists in the final executable, or that multiple identical instances are handled correctly.
// my_header.h
#ifndef MY_HEADER_H
#define MY_HEADER_H
// 'inline' allows this function to be defined in a header
// without violating ODR if included in multiple .cpp files.
inline void print_message() {
// This function's definition is available to all translation units
// that include this header.
// The compiler/linker will ensure only one actual definition is used.
std::cout << "Hello from an inline function!" << std::endl;
}
#endif // MY_HEADER_H
Using inline
in a C++ header to satisfy ODR.
In summary, inline
is a powerful tool when used judiciously. It's a suggestion for optimization and a mechanism to manage the One Definition Rule in C++. Understanding its true nature – a compiler hint rather than a command – is key to leveraging it effectively without introducing unintended performance or compilation issues.