I have two questions:
1) Why are pointers to inline functions allowed in C++? I have read that the code of inline functions just gets copied to the function call stateme
The inline keyword was originally a hint to the compiler that you the programmer think this function is a candidate for inlining - the compiler is not required to honor this.
In modern usage, it has little to nothing to do with inlining at all - modern compilers freely inline (or not) functions "behind you back", these form part of the optimization techniques.
Code transformations (including inlining) are done under the "as-if" rule in C++, which basically means that the compiler can transform the code as it wants to, so long as the execution is "as-if" the original code was executed as written. This rule fuels optimizations in C++.
That said, once an address is taken of a function, it is required to exist (i.e. the address is required to be valid). This may mean that it is no longer inlined, but could still be (the optimizer will apply the appropriate analysis).
So why can a pointer exist to a inline function, given that there is no fixed memory address of inline functions?
No, it is only a hint and largely relates to linkage and not actual inlining. This fuels, what is arguably the main current usage, defining functions in header files.
Should it not print different values of address of
n
each timefunc()
is called?
It might, the n
is a local variable, based on the stack location when the function executes. That said, the function inline
, it relates to linkage, the linker will merge the functions over the translation units.
As noted in the comments;
... that if the example is changed to
static int n
, then every call to the function must print a constant value (in a single program run of course) ... and that is true whether or not the code is inlined or not.
This is, again, the effect of the linkage requirement on the local variable n
.