inline function

The following code gives same result whether "inline" present Or not. Then what is the purpose of "inline" in the following purpose?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
using namespace std;

class square
{
private:
    int n, r;
    float n1, r1;
public:
    void input();
    void calc();
    void display();
};
inline void square::input()
{
    cout << "Enter an integer :: ";
    cin >> n;
    cout << "\nEnter a float no. :: ";
    cin >> n1;
}
inline void square::calc()
{
    r = n * n;
    r1 = n1 * n1;
}
inline void square::display()
{
    cout << "\nSquare of integer [ " << n << " ] = " << r << "\n";
    cout << "\nSquare of float [ " << n1 << " ] = " << r1 << "\n";
}
int main()
{
    square s;
    s.input();
    s.calc();
    s.display();
    return 0;
}
By "same result" do you mean it produces the same binary
Or that it has the same functional behaviour
Hello PK Dey,

Have a look at https://en.cppreference.com/w/cpp/language/inline

Andy
The implication of your question is that you're expecting different behavior based on the presence or absence of the inline keyword. Why do you think you would get a different result?


The original intent of the inline keyword was to serve as an indicator to the optimizer that inline substitution of a function is preferred over function call, that is, instead of executing the function call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids overhead created by the function call (passing the arguments and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times.

https://en.cppreference.com/w/cpp/language/inline

The net effect of the inline keyword in your example is:
31
32
33
34
35
36
37
38
39
40
41
42
int main()
{   square s;
    cout << "Enter an integer :: ";
    cin >> s.n;
    cout << "\nEnter a float no. :: ";
    cin >> s.n1;
    s.r = s.n * s.n;
    s.r1 = s.n1 * s.n1;
    cout << "\nSquare of integer [ " << s.n << " ] = " << s.r << "\n";
    cout << "\nSquare of float [ " << s.n1 << " ] = " << s.r1 << "\n";
    return 0;
}

Last edited on
The main current usage for specifying that a function not defined in a class is inline is to allow duplicate definitions in different compilation units (functions defined in a class are automatically inline).

the meaning of the keyword inline for functions came to mean "multiple definitions are permitted"


Unless you're using header files that contain function definitions that are to be included in different compilation units in the same solution - or your code is very performance sensitive - there's no benefit in using inline.

Last edited on
inline for performance tweaking is a tug-of-war battle with the compiler, where a few of the things that can happen include:

scenario 1: the compiler already decided to inline your function, so adding the keyword does nothing.

scenario 2: the compiler decided your function cannot (or even, should not) be inline, and ignored you.

there are other things that can go on as well but those two are common.
scenario 3: you told it to inline and it did, making the code actually slower than before because it was not a good choice.

visual studio has a force inline keyword that you can use if you want that extension. Other compilers may also, not sure what the commands are called.
you can also force inline with #includes (or macros, yuck) instead, but this would be a last resort and its been a long time for me since a compiler refused to inline something that needed it and the performance gains were worth the kludge force.

inline is just a suggestion to the compiler.
cppreference wrote:
The original intent of the inline keyword was to serve as an indicator to the optimizer that inline substitution of a function is preferred over function call, that is, instead of executing the function call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids overhead created by the function call (passing the arguments and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times.

Since this meaning of the keyword inline is non-binding, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline. Those optimization choices do not change the rules regarding multiple definitions and shared statics...

C++17 changed the meaning of inline considerably:
cppreference wrote:
Because the meaning of the keyword inline for functions came to mean "multiple definitions are permitted" rather than "inlining is preferred", that meaning was extended to variables.

https://en.cppreference.com/w/cpp/language/inline

The example at the link shows an inlined function and a variable (C++17 or later required). Whether the function and variable are actually inlined is up to the compiler.
Topic archived. No new replies allowed.