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?
There are two somewhat separate decisions the compiler makes concerning function inlining:
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.