C++ 内联 vs __inline vs __inline__ vs __forceinline?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2765164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:37:43  来源:igfitidea点击:

inline vs __inline vs __inline__ vs __forceinline?

c++inlinekeyword

提问by Xavier Ho

What are the differences between these four inline (key)words?

这四个内联(关键字)词有什么区别?

inline, __inline, __inline__, __forceinline.

inline, __inline, __inline__, __forceinline.

回答by kennytm

inlineis thekeyword, in C++ and C99.

inline所述关键字,在C ++和C99。

__inlineis a vendor-specific keyword (e.g. MSVC) for inline function in C, since C89 doesn't have it.

__inline是C 中内联函数的供应商特定关键字(例如MSVC),因为 C89 没有它。

__inline__is similar to __inlinebut is from another set of compilers.

__inline__类似于__inline但来自另一组编译器。

__forceinlineis another vendor-specific (mainly MSVC) keyword, which will apply more force to inline the function than the __inlinehint (e.g. inline even if it result in worse code).

__forceinline是另一个特定于供应商(主要是 MSVC)的关键字,它将比__inline提示施加更多的力量来内联函数(例如内联,即使它会导致更糟糕的代码)。

There's also __attribute__((always_inline))in GCC and clang.

__attribute__((always_inline))在 GCC 和 clang 中也有。

回答by CB Bailey

__inline, __inline__and __forceinlineare all implementation specific. Because of the double underscore they are all identifiers reserved for the implementation so shouldn't conflict with identifiers used in applications.

__inline__inline__并且__forceinline都是特定于实现的。由于双下划线,它们都是为实现保留的标识符,因此不应与应用程序中使用的标识符冲突。

inlineis the only C++ keyword.

inline是唯一的 C++ 关键字。

回答by Holger Kretzschmar

For the Visual Studio compiler it means:

对于 Visual Studio 编译器,这意味着:

  • inline- suggestion to the compiler to inline your code

  • __forceinline- overrides the builtin compiler optimization and generates inline code

  • 内联- 建议编译器内联您的代码

  • __forceinline- 覆盖内置编译器优化并生成内联代码

For more details see: http://msdn.microsoft.com/en-us/library/z8y1yy88%28VS.71%29.aspx

有关更多详细信息,请参阅:http: //msdn.microsoft.com/en-us/library/z8y1yy88%28VS.71%29.aspx