2

I'm learning c++ via this page.

In the page above and this page, it says that a modern compiler is smart enough to decide which side will have the better performance regarding the inlining of the function. The compiler is smarter than a human in most cases.

So, I thought that we could use the inline keyword if we want to force the compiler to treat the function as inline, but that was not the case. It says that even if the function is declared as inline, the compiler is free to (and usually does) ignore the keyword.

Does this mean that the function's inline-ness is not decided by the inline keyword, but rather the compiler has the ultimate privilege to decide it? If so, then isn't it useless for a programmer to explicitly declare a function as inline?

pro-gramer
  • 166
  • 1
  • 3
  • 14
MyBug18
  • 2,135
  • 2
  • 11
  • 25
  • 1
    There are other effects: https://en.cppreference.com/w/cpp/language/inline – eesiraed Apr 03 '20 at 05:01
  • Inline allows a function to be defined in multiple translation units. –  Apr 03 '20 at 05:03
  • `inline` doesn't have anything to do with inlining anymore. If you can understand the cppreference page, you'll see that `inline` has everything to do with linkage. So your first linked page is flat out wrong. – Zuodian Hu Apr 03 '20 at 05:06

2 Answers2

3

On the pretext on when it was defined, it seemed to be a good optimization, however it doesn't hold any value now since C++17.

What it implies now is that it can have multiple identical definitions, and needs to be defined in every translation unit that uses it.

Does it mean that the function's inline-ness is not decided by the inline keyword, but the compiler has the entire privilege to decide it?

Yes. It doesn't depend on whether you use inline or not as well. Compilers can self-identify whether to inline a function or not.

If so, then isn't it useless for a programmer to explicitly declare function as inline?

Refer to its usage mentioned above for inline functions. Pertaining to the question title, the inline keyword is applicable to inline variables (C++ 17) as well, which allows such variables to be defined in multiple translation units, following the one definition rule. If defined more than once, the compiler merges them all into a single object in the final program.

2

The inline keyword has nothing to do with inlining calls to a function. All it does is allow the function (or object, as of C++17) to be defined in multiple compilation units. That is, it allows a function to be defined inline in a header where it's declared.

This may assist the compiler in being able to inline calls to a function, since the full definition of a function defined in a header is visible in multiple translation units. The inline keyword is not a requirement, or even a hint, that the compiler should inline the calls. The compiler will make that determination on its own.


There is no standard way to force the compiler to inline calls to a function, but most compilers provide an extension to do so. For example, GCC has the always_inline attribute.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • I think I am still lack of the understanding of this "translation unit" thing, but I got your point. Thank you :) – MyBug18 Apr 03 '20 at 05:19