What is wrong with using inline functions?

后端 未结 12 1288
滥情空心
滥情空心 2020-11-29 01:51

While it would be very convenient to use inline functions at some situations,

Are there any drawbacks with inline functions?

Conclusion:

12条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 02:08

    As others have mentioned, the inline keyword is only a hint to the compiler. In actual fact, most modern compilers will completely ignore this hint. The compiler has its own heuristics to decide whether to inline a function, and quite frankly doesn't want your advice, thank you very much.

    If you really, really want to make something inline, if you've actually profiled it and looked at the disassembly to ensure that overriding the compiler heuristic actually makes sense, then it is possible:

    • In VC++, use the __forceinline keyword
    • In GCC, use __attribute__((always_inline))

    The inline keyword does have a second, valid purpose however - declaring functions in header files but not inside a class definition. The inline keyword is needed to tell the compiler not to generate multiple definitions of the function.

提交回复
热议问题