Inline Functions
I know compiler may or may not perform inline expansion of a function whether requested by th开发者_如何学JAVAe programmer or not.
I was just curious to know, is there any way by which programmer can know for sure that compiler has inlined a particular function?Other than by looking at the generated code, no. Some implementations may provide that information but it's not required by the standard.
Things like inline
or register
(shudder) are suggestions to the compiler and it's free to accept them, ignore them or even lie to you that it's done it while secretly going behind your back and not doing it :-)
I tend not to use features like that since I suspect the compiler often knows better than I do how to wring the most performance out of my code.
You can profile your code and see if the function of interest shows up in the call stack. Although, I suppose there is no guarantee if your stack sampling rate is not high enough.
But it may prove that it is inlined: if you know A
calls B
, which calls C
, and A
never calls C
directly, if you see A
calling C
on the call stack, you know B
was inlined for that call.
Set your compiler to generate assembler code and check there.
Read the disassembly of the object file.
There is no way to know except to look at the output assembler.
Compilers these days are 'smart' and they decide what functions to inline and in what cases.
Just like the register keyword, compilers do the picking these days and really ignore your requests.
I don't think there is a way to find out what you want,
But you can increase the possibilites of the function being an inline function by,
Making the definition of the function visible to the translation unit in which it is called. i.e you always have to put the definition of an inline function in header file.
精彩评论