Question

I'm trying to experiments with inline function. I think that the substitution of function's body executed at preprocessing time. But it is not true. If we declare inline function as the follwoing

//--main.cpp--//
....
inline void bar();
....

and running g++ -E main.cpp then we can to see inline function without changes. So when does substitution body function executed?

Was it helpful?

Solution

Funcion inlining is a compile time action. Note, inline is not a command, it is a request to compiler, and compiler is free to ignore it. For example, large methods, recursive methods, methods containing loops or other method invocation are usually not inlined. When compiler do performs inlining, it replaces method call with method body, somewhat similar to macro expansion, but this process is more complex. Compiler does not blindly replace method invocation unlike macro, it must take care of parameters to the method.

According to the standard

A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The 
inline specifier indicates to the implementation that inline substitution of the function body at the  
point of call is to be preferred to the usual function call mechanism. An implementation is not required  
to perform this inline substitution at the point of call; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top