Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usefulness of the "inline" feature

Tags:

c++

There's two things about inlining:

  • The inline keyword will be ignored if the compiler determines that the function cannot be inlined.
  • There is a compiler optimization (on Visual Studio, I don't know about GCC) that tells the compiler to inline all functions where possible.

From this I conclude that I never need to bother about inlining. I just have to turn on the compiler optimization for the release build.

Or are there any situations where manually inlining would be preferred?

like image 769
StackedCrooked Avatar asked Nov 30 '22 05:11

StackedCrooked


1 Answers

The inline keyword has two functions:

  • it serves as a hint to the compiler to perform the inlining optimization (this is basically useless on modern compilers, which inline aggressively with or without the keyword)
  • it tells the compiler/linker to ignore the One Definition Rule: that the inline'd symbol may be defined in multiple translation units (typically because it is defined in a header, that is included from multiple files). Normally, this would result in a linker error, but it is allowed when you use the inline keyword.
like image 175
jalf Avatar answered Dec 15 '22 13:12

jalf