使用 GCC 的 C++ 内联函数 - 为什么是 CALL?

标签 c++ inline tdm-mingw

我一直在测试 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 指令。

alt text

为什么会这样?那我该如何使用内联函数呢?

编辑:使用的命令行选项:

**** 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 inline docs

关于使用 GCC 的 C++ 内联函数 - 为什么是 CALL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/934529/

相关文章:

c++ - 为什么 typename 是必需的,即使它似乎足以推断名称应该是一种类型?

c++ - C++中模板对象的动态数组

c++ - SFML - 按下键时呈现字符?

c - 错误 : expected initializer before "BOOL CALLBACK _export Function" in Bloodshed Dev-C++ 5. 11

c++ - 内联还有用吗?

c++ - 特化模板<typename T, template<typename> class U>

c++ - 提取 "invokables"的签名

kotlin - 你能帮我解释一下这个功能是如何工作的吗?它使用 Inline、reified、valueOf() 和 enumValues

html - 获取两个内联(或内联 block )元素以匹配高度