A time when inline operation cannot be used

Im exploring inline functions in C ++ and falls into the section on the limitations of its use. It says:

The compiler also cannot perform insertion if the address of the function is accepted implicitly or explicitly.

Can someone explain to me, perhaps with some example, what exactly does this mean?

+6
source share
3 answers

There are two several separate decisions that the compiler makes regarding a function:

  • whether a specific function call is built in;
  • Is there a non-built-in version of the function?

The first is determined by the compiler in each case, if insertion is possible at this point. This will not be possible if the function is virtual or called through the function pointer, and it cannot determine at compile time which function to call. This is not possible if the definition is not available to the compiler, possibly because it is defined in a different translation unit, and the compiler does not perform "optimization of the entire program". The decision may or may not be influenced by whether the inline function is declared, and other factors, such as the size and frequency of its call.

The second depends on whether a non-embedded version is required. This will be required if any call is not attached to it. It will also be (according to your quote) if something needs a function address, since then it must have an address. This can happen either directly (for example, by assigning an address to a function pointer), or indirectly (for example, virtual functions will need their address stored somewhere to search at runtime according to the dynamic type of the object).

The existence of a non-built-in version will not prevent any particular function call from being built-in, although it is possible that this may affect the decision of the compiler, especially if it is configured to optimize for code size.

To summarize, your quote is simplified and not entirely accurate; the compiler can still "perform inlining", if the address is done, it simply cannot omit the non-embedded version.

+5
source

You can mark any function as built-in. Even a virtual function, even a recursive function, even a long-lived veeery function, even if its address is busy. The main difference between a built-in and a non-built-in function is that the definition of the former must appear in each translation unit (as the source file) in which it is used (which is why the built-in functions are usually defined in the .h file), while the latter should only be determined once. You can use the built-in function in any way to use the non-built-in.

The actual component depends on the compiler. It may ignore your request if, for example, your function is recursive or too long. On the other hand, the compiler can choose a built-in function that you have not actually marked as built-in.

+6
source

This is simply wrong: the ability to call a function inline does not depend on evaluating 2 + 2 or by addressing the function somewhere.

What book or article are you reading?

On the other hand, if the address is done, then it may be almost impossible to remove a separate machine code function.

Cheers and hth.,

+4
source

Source: https://habr.com/ru/post/892057/


All Articles