我一直在测试 C++ 中的内联函数调用。
Thread model: win32
gcc version 4.3.3 (4.3.3-tdm-1 mingw32)
C++ 编程语言中的 Stroustrup 写道:
The inline specifier is a hint to the compiler that it should attempt to generate code [...] inline rather than laying down the code for the function once and then calling through the usual function call mechanism.
但是,我发现生成的代码根本不是内联的。 isquare 函数有一个 CALL 指令。
为什么会这样?那我该如何使用内联函数呢?
编辑:使用的命令行选项:
**** Build of configuration Debug for project InlineCpp ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\InlineCpp.o ..\src\InlineCpp.cpp
g++ -oInlineCpp.exe src\InlineCpp.o
最佳答案
就像 Michael Kohne 提到的那样,inline 关键字始终是一个提示,对于您的函数,GCC 决定不内联它。
由于您使用的是 Gcc,您可以使用 __attribute((always_inline)) 强制内联。
例子:
/* Prototype. */
inline void foo (const char) __attribute__((always_inline));
关于使用 GCC 的 C++ 内联函数 - 为什么是 CALL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/934529/