Does GCC inline C++ functions without the 'inline' keyword?

13,974

Solution 1

Yes. Any compiler is free to inline any function whenever it thinks it is a good idea. GCC does that as well.

At -O2 optimization level the inlining is done when the compiler thinks it is worth doing (a heuristic is used) and if it will not increase the size of the code. At -O3 it is done whenever the compiler thinks it is worth doing, regardless of whether it will increase the size of the code. Additionally, at all levels of optimization (enabled optimization that is), static functions that are called only once are inlined.

As noted in the comments below, these -Ox are actually compound settings that envelop multiple more specific settings, including inlining-related ones (like -finline-functions and such), so one can also describe the behavior (and control it) in terms of those more specific settings.

Solution 2

Yes, especially if you have a high level of optimizations enabled.

There is a flag you can provide to the compiler to disable this: -fno-inline-functions.

Solution 3

If you use '-finline-functions' or '-O3' it will inline functions. You can also use '-finline_limit=N' to tune how much inlining it does.

Solution 4

Yes, it does, although it will also generate a non-inlined function body for non-static non-inline functions as this is needed for calls from other translation units.

For inline functions, it is an error to fail to provide a function body if the function is used in any particular translation unit so this isn't a problem.

Share:
13,974
Carl Seleborg
Author by

Carl Seleborg

Software developer in Berlin at Ableton (a really cool place to work!), proud to hack away on our product Live, a music sequencer and performance tool for electronic musicians. I've been programming professionally for about 5 years, and am deeply in love with C++ which, just like the German language, provides enough obscure corners and crazy surprises to make every day a new challenge!   I also write a blog (in French), called 5h du matin.

Updated on June 02, 2022

Comments

  • Carl Seleborg
    Carl Seleborg about 2 years

    Does GCC, when compiling C++ code, ever try to optimize for speed by choosing to inline functions that are not marked with the inline keyword?