> an inline function will be compiled in the body where it was called and thus the finished
> program won't be making a function call at all.
The only binding semantics for an inline function with external linkage is:
it can be defined (identically) in multiple translation units; it must be defined in every translation unit in which it is used; the behavior of the program is as if there is exactly one definition of the function in the entire program.
Inline substitution of the function body at the point of call is a non-binding requirement; as far as inline substitution goes, the typical optimiser does not treat functions marked inline any differently from functions which are not.
Re. inline variables (C++17)
The original intent of the inline keyword was to serve as an indicator to the optimizer that inline substitution of a function is preferred over function call ...
Because the meaning of the keyword inline for functions came to mean "multiple definitions are permitted" rather than "inlining is preferred", that meaning was extended to variables.
http://en.cppreference.com/w/cpp/language/inline |
> My question though is, at what point is it better for the final code to be a function rather than inlined?
> Is it purely a matter of the size of the final code (including multiple copies for every time a function is called),
> or is there something else beyond that?
A good candidate for inline substitution would have the following properties:
a. It is stable (it is unlikely that the implementation of the function would change) -
b. It is called a large number of times during typical program execution.
c. If the function is called from many different places in the code, the inline-substituted code size is comparable to (or at least not much larger than) the code size that would be required for setting up a non-inline call.
> the compiler only has the option of inlining code in the header, but not a source file linked by a header. ...
> I would need to write code in the header files just to give the compiler the option
> of being smarter then me about when to inline.
We can enable link-time code generation; with that cross-translation-unit inline substituion is possible.
Microsoft: -LTCG (enabled by default in 'Release' builds)
https://docs.microsoft.com/en-us/cpp/build/reference/ltcg-link-time-code-generation
GNU / GNU compatible: -flto (caveat: object file sizes and build times shoot up several-fold)
https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Optimize-Options.html#Optimize-Options