C++ inline functions using GCC - why the CALL?

2019-02-02 08:27发布

I have been testing inline function calls in C++.

Thread model: win32
gcc version 4.3.3 (4.3.3-tdm-1 mingw32)

Stroustrup in The C++ Programming language wirtes:

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.

However, I have found out that the generated code is simply not inline. There is a CALL instrction for the isquare function.

alt text http://i42.tinypic.com/8ys3f4.jpg

Why is this happening? How can I use inline functions then?

EDIT: The command line options used:

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

7条回答
女痞
2楼-- · 2019-02-02 08:53

Are you looking at a debug build (optimizations disabled)? Compilers usually disable inlining in "debug" builds because they make debugging harder.

In any case, the inline specified is indeed a hint. The compiler is not required to inline the function. There are a number of reasons why any compiler might decide to ignore an inline hint:

  • A compiler might be simple, and not support inlining
  • A compiler might use an internal algorithm to decide on what to inline and ignore the hints.
    (sometimes, the compiler can do a better job than you can possibly do at choosing what to inline, especially in complex architectures like IA64)
  • A compiler might use its own heuristics to decide that despite the hint, inlining will not improve performance
查看更多
登录 后发表回答