Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do c++11-compatible compilers always ignore inline hints?

Tags:

c++

c++11

inline

Reading an old answer on When should I write the keyword 'inline' for a function/method? that says:

It is said that inline hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.

This answer was posted in 2009 so I want to figure it finally out:

  1. Do modern c++11-compatible compilers always ignore inline hints specified by user and do this only automatically?
  2. Do inline hints only stay to provide backward compatibility?
  3. If not 1. so this answer is incorrect?
like image 226
Victor Polevoy Avatar asked Aug 17 '15 09:08

Victor Polevoy


3 Answers

  1. No, they just interpret it as the standard requires, but some compilers may do nothing more than that (according to the documentation MSVC chooses based on the presence of inline keyword though, also GCC 5.1.0 takes the inline keyword into consideration when deciding).
  2. No, inline is needed to avoid duplicate symbols when linking too.

The inline keyword does have meaning, but not the meaning you may expect. It doesn't mean that the compiler must/should/might inline expand the function, that the compiler may decide to do as it sees fit, no matter if you use inline or not.

What it does mean is that you should and could repeat the function definition in every compilation unit where it is used, without resulting in linking errors - quite similar to the static keyword.

For example GCC 4.7.2 (which is perhaps not state-of-the-art, but still quite a modern compiler) seem to interpret inline no more than the standard specifies. It does not seem to inline the function if optimization is disabled and with optimization turned on it seem to inline as it likes anyway. The only difference is how the compiler handles the "outlined" function in the different cases, with inline it simply discard it or handles it in a way that would avoid duplicate symbols at link time.

like image 100
skyking Avatar answered Nov 15 '22 09:11

skyking


  1. Do modern c++11-compatible compilers always ignore inline hints specified by user and do this only automatically?

C++11 is irrelevant here, the C++11 standard did not change the semantics of inline and compiler optimisations are largely independent of the language version being compiled.

  1. Do inline hints only stay to provide backward compatibility?

No, inline is not a hint, and the compiler doesn't "ignore" inline because if it did that you'd get multiple definition errors. The meaning the compiler gives to the inline keyword is not the meaning you seem to understand. It's not a hint.

If the compiler can't see the function definition it can't inline it (Link-Time Optimization changes that, but use of LTO is not very widespread yet and most libraries do not ship with LTO-enabled binaries that would allow link-time inlining).

You should read inline as "this function definition appears inline in this file" not "calls to this function should be inlined".

So the inline keyword is useful for allowing the compiler to see function definitions in multiple files, which means it is possible for it to optimise the calls away by inlining them. That doesn't necessarily make it more likely to be inlined than any other function defined in the same translation unit.

For functions that are called from multiple translation units defining the functions in headers and making them inline is necessary condition for the compiler to inline them, but it is not sufficient (because the compiler bases its inlining decisions on other conditions).

This has nothing to do with backwards compatibility, it is just as true today as in 2009.

like image 43
Jonathan Wakely Avatar answered Nov 15 '22 09:11

Jonathan Wakely


C++11 compilers follow inlining hints just the same as C++03 or any edition of the language.

However, note that the inline keyword is not an inlining hint. Hints look more like __attribute__((always_inline)).

The inline keyword (or implicit inline status, which is applied to templates and such) indicates that a function is defined in a header, so the linker should expect to see multiple copies of it when combining translation units (.cpp files). It is so named because the definition must be available in order to inline a function, which means that functions in libraries need inline to practically be inlined.

like image 28
Potatoswatter Avatar answered Nov 15 '22 10:11

Potatoswatter