内联函数Inline Functions

Microsoft 专用Microsoft Specific

__inline 关键字告诉编译器用函数定义中的代码替换每个函数调用实例。The __inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. 但是,替换操作完全由编译器自行决定。However, substitution occurs only at the compiler's discretion. 例如,如果某个函数的地址被采用或者由于过大而无法内联,则编译器不会内联该函数。For example, the compiler does not inline a function if its address is taken or if it is too large to inline.

对要作为内联候选项加以考虑的函数,它必须使用新式的函数定义。For a function to be considered as a candidate for inlining, it must use the new-style function definition.

请使用以下形式指定内联函数:Use this form to specify an inline function:

__inline typeopt function-definition__inline typeopt function-definition

使用内联函数将生成更快的代码,有时可能生成比等效函数调用所生成的更小的代码,原因如下:The use of inline functions generates faster code and can sometimes generate smaller code than the equivalent function call generates for the following reasons:

  • 它节省了执行函数调用所需的时间。It saves the time required to execute function calls.

  • 小的内联函数(可能是三行或更少)创建的代码比等效函数调用创建的代码更少,因为编译器不会生成处理参数和返回值的代码。Small inline functions, perhaps three lines or less, create less code than the equivalent function call because the compiler doesn't generate code to handle arguments and a return value.

  • 函数生成的内联需要进行普通函数不可用的代码优化,因为编译器不执行过程间优化。Functions generated inline are subject to code optimizations not available to normal functions because the compiler does not perform interprocedural optimizations.

使用 __inline 的函数不应与内联汇编常程序代码相混淆。Functions using __inline should not be confused with inline assembler code. 有关详细信息,请参阅内联汇编程序See Inline Assembler for more information.

结束 Microsoft 专用END Microsoft Specific

请参阅See Also

inline、__inline、__forceinlineinline, __inline, __forceinline