Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Times when inline functioning cannot be used

Im studying inline functions in C++ and have come to a section concerning limitations of its use. It says:

The compiler also cannot perform inlining if the address of the function is taken implicitly or explicitly.

Can someone explain to me, perhaps with an example of some sort, what exactly this means?

like image 537
Bap Johnston Avatar asked Jul 05 '11 16:07

Bap Johnston


2 Answers

You can mark any function as inline. Even a virtual function, even a recursive function, even a veeery veery long function, even if its address is taken. The main difference between an inline and non-inline function is that the definition of the former must appear in every translation unit (aka source file) in which it is used ( that's why inline functions are usually defined in the .h file), whereas the latter must be defined only once. You can use the inline function in every way that you can use a non-inline one.

The actual inlining part is up to the compiler. It can ignore your request, if, for example, your function is recursive or too long. On the other hand, the compiler may choose to inline a function which you haven't actually marked as inline.

like image 199
Armen Tsirunyan Avatar answered Sep 28 '22 18:09

Armen Tsirunyan


There are two somewhat separate decisions the compiler makes concerning function inlining:

  • whether a particular function call is inlined;
  • whether a non-inline version of the function exists.

The first is decided by the compiler on a case-by-case basis, if inlining is possible at that point. It won't be possible if the function is virtual, or called through a function pointer, and it can't determine at compile time which function is to be called. It won't be possible if the definition is not available to the compiler, perhaps because it is defined in a different translation unit and the compiler does not do "whole program optimisation". The decision may, or may not, be influenced by whether the function is declared inline, and other factors such as its size and how often it is called.

The second depends on whether a non-inline version is required. It will be required if any call to it is not inlined. It will also (as per your quotation) be required if anything needs the address of the function, as then it must have an address. This can happen either directly (for example by assigning the address to a function pointer), or indirectly (for example, virtual functions will need their address stored somewhere to look up at runtime according to the object's dynamic type).

The existence of the non-inline version will not prevent any particular call to the function from being inlined, although it's possible that it might influence the compiler's decision, particularly if it's configured to optimise for code size.

To summarise, your quotation is simplistic and not entirely accurate; the compiler can still "perform inlining" if the address is taken, it just can't omit the non-inline version.

like image 40
Mike Seymour Avatar answered Sep 28 '22 18:09

Mike Seymour