views:

118

answers:

1

here is a small question about inline functions in c++.

At what stage of the compilation in C++ are the inline functions actually inlined at the call?

how does that basically work.

lets say if the compiler has decided that a particualr function has to be inline after the programmer has requested with an inline keyword in front of the function ,when does the compiler does that for the programmer .i mean at what stage of the compilation.

is it at the preprocessing stage like in c macros are expanded?

+4  A: 

It will vary by compiler. And some stages in some compilers will have no corresponding stages in other compilers. So your question doesn't really have a definite answer.

But generally it's done after the parse tree for the function is created, but before code is actually generated or many optimizations are done. This is the most optimum place to do it because you want the maximum amount of information available for optimizer to work with.

Doing it like a preprocessor macro expansion would be generally too early. The compiler doesn't then have enough information to do the appropriate type checking, and it's easier also to make mistakes that cause side effects to happen more than once and so on.

And GMan provided an excellent Wikipedia link in a comment that goes into much more detail about the function inlining process than I do here. My answer is generally true, but there is a LOT of variation, even more than I thought there was.

Omnifarious