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

后端 未结 7 633
感动是毒
感动是毒 2020-12-28 17:55

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++ Program

相关标签:
7条回答
  • 2020-12-28 17:58

    It is a hint and the complier can choice to ignore the hint. I think I read some where that GCC generally ignore it. I remeber hearing there was a flag but it still does not work in 100% of cases. (I have not found a link yet).

    Flag: -finline-functions is turned on at -O3 optimisation level.

    0 讨论(0)
  • 2020-12-28 18:03

    Inline is nothing more than a suggestion to the compiler that, if it's possible to inline this function then the compiler should consider doing so. Some functions it will inline automatically because they are so simple, and other functions that you suggest it inlines it won't because they are to complex.

    Also, I noticed that you are doing a debug build. I don't actually know, but it's possible that the compiler disables inlining for debug builds because it makes things difficult for the debugger...

    0 讨论(0)
  • 2020-12-28 18:08

    I faced similar problems and found that it only works if the inline function is written in a header file.

    0 讨论(0)
  • 2020-12-28 18:09

    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
    0 讨论(0)
  • 2020-12-28 18:16

    Like Michael Kohne mentioned, the inline keyword is always a hint, and GCC in the case of your function decided not to inline it.

    Since you are using Gcc you can force inline with the __attribute((always_inline)).

    Example:

     /* Prototype.  */
     inline void foo (const char) __attribute__((always_inline));
    

    Source:GCC inline docs

    0 讨论(0)
  • 2020-12-28 18:17

    Whether to inline is up to the compiler. Is it free to ignore the inline hint. Some compilers have a specific keyword (like __forceinline in VC++) but even with such a keyword virtual calls to virtual member functions will not be inlined.

    0 讨论(0)
提交回复
热议问题