Paul Yang
Paul Yang

Reputation: 346

is inline still necessary since compilers will do inline automatically

here is the code a1.cpp

#include <stdio.h>

void func(void)
{
        printf("hello world\n");
}

int main()
{

        func();
        return 0;
}

here is the code a2.cpp

#include <stdio.h>

inline void func(void)
{
        printf("hello world\n");
}

int main()
{

        func();
        return 0;
}

they are only different with the key word 'inline'

then I compile them into assemble

g++ -S a1.cpp
g++ -S a2.cpp

the result is: inline does not work. function call remained in main.

then I compile them with optimizer

g++ -O2 -S a1.cpp
g++ -O2 -S a2.cpp

the result is : inline works in a2.s, but a1.s also replace the function call. it seems inline was added automatically.

so when inline is necessary when coding.

Upvotes: 2

Views: 1324

Answers (2)

eerorika
eerorika

Reputation: 238311

so when inline is necessary when coding.

Short answer: Probably never (and never has been).

Long answer: Depends on your definition of "necessary". If it is necessary that some function call is expanded inline, and it is necessary to call that function from multiple translation units and the function cannot have internal linkage for whatever reason and you have to use an old compiler that doesn't support link time optimisation or that optimisation cannot be enabled for some other reason, then perhaps you have a case where it is necessary to define a function inline. Such cases are probably quite rare.

Note that since in this hypothetical case it is necessary for the calls to be expanded inline, you would necessarily also need some other technique to enforce that inline expansion. No standard way exists but there are language extensions.

Although they may not be necessary, inline definitions are used mostly because they are convenient and simple, especially in case of variables.


Note that there are two common (and contradictory) misconceptions about inline functions:

  • That functions expand only calls to inline functions and/or that they do not expand calls to non-inline functions. You've proven this misconception wrong.
  • That defining a function inline has nothing to do whether a function call may be expanded inline. It does have something to do with it: A compiler can expand a function call inline only if the function is defined in the same translation unit (this limitation does not apply to linkers). Since a non-inline function can only be defined in one TU, so can calls to it be inlined only within one TU (by a compiler). Furthermore, compilers do use inline declaration as a heuristic for determining whether to expand a call inline. Even today. At least based on source code; it could technically be dead code that is never called.

Upvotes: 0

Miles Budnek
Miles Budnek

Reputation: 30494

All the inline keyword does is allow a variable or function to be defined in multiple translation units without violating the One Definition Rule so long as all of the definitions are identical. It has absolutely nothing to do with inlining calls to a function. What it does is allow a function (or variable, since C++17) to be defined inline in a header file that gets included into multiple translation units instead of having to declare the function in the header and define it externally in exactly one translation unit.

Consider the following program:

header.hpp

#include <iostream>

void foo() {
    std::cout << "hello world\n";
}

a.cpp

#include "header.hpp"

b.cpp

#include "header.hpp"

int main() {}

This program violates the One Definition Rule because foo is defined in two translation units. Its is thus ill-formed, no diagnostic required. Add the inline keyword to foo and this program will become correct and well defined. foo will still be defined in multiple translation units, but since it's inline and those definitions are identical, the One Definition Rule is not violated.


Note that defining a function in a header like this may aid the compiler in inlining calls to the function (since the function's definition is visible at the point of the call in multiple translation units). It's in no way required though. Most modern compiler/linker toolchains implement some form of link-time optimization that can inline calls to functions defined in other translation units.

Upvotes: 6

Related Questions