简体   繁体   中英

Inline Functions

I know compiler may or may not perform inline expansion of a function whether requested by the 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.

设置编译器以生成汇编代码并检查。

阅读目标文件的反汇编。

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. ie you always have to put the definition of an inline function in header file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM