我如何知道函数是否已内联?

Rak*_*wal 21 c++ inline

如果我将任何函数标记为内联,是否有一种方法可以知道函数是否内联?

Nik*_*sov 27

使用GCC,您可以使用-Winline编译器选项:

  -Winline  Warn if a function can not be inlined and it was declared as inline.

mangcc 的文件继续说:

  Even with this option, the compiler will not warn about
  failures to inline functions declared in system headers.

  The compiler uses a variety of heuristics to determine whether or
  not to inline a function.  For example, the compiler takes into
  account the size of the function being inlined and the amount of
  inlining that has already been done in the current function.
  Therefore, seemingly insignificant changes in the source program
  can cause the warnings produced by -Winline to appear or disappear.


小智 9

查看编译器发出的汇编语言.例如,使用g ++进行编译:

g++ -S -c foo.c
Run Code Online (Sandbox Code Playgroud)

将创建一个名为foo.s的文件,其中包含汇编语言输出.或者,再次使用GCC工具集,使用objdump:

g++ -c foo.c
objdump -d foo.o
Run Code Online (Sandbox Code Playgroud)

其他工具集具有类似的功能.


Mar*_*ett 5

1,看看汇编输出
2,你为什么关心?

  • @Rakesh:在编译时完全检查访问说明符.在运行时,不再有公共或私有,只是在处理器周围的字节. - 此外,类定义中的实现无论如何都是"内联"的(因为关键字主要意味着"让链接器忽略对此的多个定义(以便该函数可以在头文件中实现,大概是为了使**可以*内联) ". (5认同)