C++ inline Keyword
The inline
keyword in C++ is used to suggest to the compiler that the function’s code should be inserted directly at the call site, rather than being invoked through the usual function call mechanism. This can reduce the overhead of function calls and improve performance, especially for small, frequently used functions.
Syntax
</>
Copy
inline return_type function_name(parameters) {
// Function body
}
- inline
- The keyword indicating the function is suggested for inlining.
- return_type
- The return type of the function.
- function_name
- The name of the function to be inlined.
- parameters
- The input parameters for the function.
Examples
Example 1: Inline Function for Simple Calculation
In this example, we will learn how to use an inline function that calculates the square of a number.
</>
Copy
#include <iostream>
using namespace std;
inline int square(int x) {
return x * x;
}
int main() {
cout << "Square of 5: " << square(5) << endl;
cout << "Square of 7: " << square(7) << endl;
return 0;
}
Output:
Square of 5: 25
Square of 7: 49
Explanation:
- The function
square
is marked asinline
. - When
square(5)
andsquare(7)
are called, the compiler may replace these calls with the function’s code directly. - This eliminates the overhead of a function call.
Example 2: Inline Function in a Class
In this example, we will learn how to use an inline member function in a class.
</>
Copy
#include <iostream>
using namespace std;
class Rectangle {
private:
int length, width;
public:
Rectangle(int l, int w) : length(l), width(w) {}
inline int area() {
return length * width;
}
};
int main() {
Rectangle rect(5, 3);
cout << "Area: " << rect.area() << endl;
return 0;
}
Output:
Area: 15
Explanation:
- The
area
member function is defined asinline
inside the class. - The compiler may inline the function when
rect.area()
is called, replacing the call with the function’s code. - This reduces the overhead of a function call for frequently used member functions.
Example 3: Inline Function with Default Arguments
In this example, we will learn how to use an inline function with default parameters.
</>
Copy
#include <iostream>
using namespace std;
inline int add(int a, int b = 5) {
return a + b;
}
int main() {
cout << "Sum (5, 5): " << add(5) << endl;
cout << "Sum (3, 7): " << add(3, 7) << endl;
return 0;
}
Output:
Sum (5, 5): 10
Sum (3, 7): 10
Explanation:
- The inline function
add
has a default parameterb
with a value of5
. - When
add(5)
is called, the second argument defaults to5
. - The compiler may inline the function to avoid the overhead of a function call.
Key Points about inline Keyword
- The
inline
keyword suggests to the compiler to replace the function call with the function’s code at the call site. - It reduces the overhead of function calls but can increase code size due to duplication.
- It is typically used for small, frequently called functions.
- Inline functions can be defined inside a class or outside with the
inline
keyword. - Modern compilers often decide inlining during optimization, even without the explicit
inline
keyword.