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:

  1. The function square is marked as inline.
  2. When square(5) and square(7) are called, the compiler may replace these calls with the function’s code directly.
  3. 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:

  1. The area member function is defined as inline inside the class.
  2. The compiler may inline the function when rect.area() is called, replacing the call with the function’s code.
  3. 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:

  1. The inline function add has a default parameter b with a value of 5.
  2. When add(5) is called, the second argument defaults to 5.
  3. The compiler may inline the function to avoid the overhead of a function call.

Key Points about inline Keyword

  1. The inline keyword suggests to the compiler to replace the function call with the function’s code at the call site.
  2. It reduces the overhead of function calls but can increase code size due to duplication.
  3. It is typically used for small, frequently called functions.
  4. Inline functions can be defined inside a class or outside with the inline keyword.
  5. Modern compilers often decide inlining during optimization, even without the explicit inline keyword.