inline, __inline, __forceinline MSDN翻译

来源:互联网 发布:图片动画制作软件 编辑:程序博客网 时间:2024/05/17 02:04

inline, __inline, __forceinline

Visual Studio 2010
Other Versions
0 out of 1 rated this helpful Rate this topic

The inline and __inline specifiers instruct the compiler to insert a copy of the function body into each place the function is called.

inline 和 __inline指令通知编译器将函数体中的代码整体插入到每一个该函数被调用的地方。

inline function_declarator;   __inline function_declarator;   // Microsoft Specific__forceinline function_declarator;   // Microsoft Specific
Remarks

The insertion (called inline expansion or inlining) occurs only if the compiler's cost/benefit analysis show it to be profitable. Inline expansion alleviates the function-call overhead at the potential cost of larger code size.

这种插入(叫做inline扩展或者inlining)只有在编译器分析这种开销能够得到一定的好处时才会发生。如果代码尺寸比较大时编译器会限制这种插入。


The __forceinline keyword overrides the cost/benefit analysis and relies on the judgment of the programmer instead. Exercise caution when using __forceinline. Indiscriminate use of __forceinline can result in larger code with only marginal performance gains or, in some cases, even performance losses (due to increased paging of a larger executable, for example).

__forceinline关键字忽视了开销/受益分析而是完全信赖程序员自己的判断。但是使用__forceinline要当心。不分场合的使用__forceinline可能会导致只有细微的性能增加,或者在某些场合,甚至性能下降(比如可执行程序体积变大,程序页的数量增加)


Using inline functions can make your program faster because they eliminate the overhead associated with function calls. Functions expanded inline are subject to code optimizations not available to normal functions.

使用inline功能可以使程序更快,因为inline可以省去函数调用带来的开销。但函数的inline扩展受到编译器代码优化的限制不适用于一般的函数。


The compiler treats the inline expansion options and keywords as suggestions. There is no guarantee that functions will be inlined. You cannot force the compiler to inline a particular function, even with the __forceinline keyword. When compiling with /clr, the compiler will not inline a function if there are security attributes applied to the function.

编译器只是把inline扩展选项和指令当做一种建议。它并不保证函数一定会被内联。你没有办法强制编译器一定使某个函数被内联,即使使用__forceinline关键字也不一定。当使用/clr选项编译时,编译器将不会内联应用有安全属性的函数。


The inline keyword is available only in C++. The __inline and __forceinline keywords are available in both C and C++. For compatibility with previous versions,_inline is a synonym for __inline.

inline关键字只用在C++。__inline 和 __forceinline关键字C和C++都可以应用。为了兼容以前的版本,_inline和__inline是等同的。


The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function (instantiate) and create standard calling linkages instead of inserting the code inline. Two cases where this can happen are:

inline关键字告诉编译器,inline扩展希望被使用。然而,编译器可能另外创建一个函数并创建一个标准的调用连接,而不是内联插入代码。有两种情况这种事情会发生:

  • Recursive functions.

    递归函数

  • Functions that are referred to through a pointer elsewhere in the translation unit.

    在编译单元中函数被别处的指针引用

These reasons may interfere with inlining, as may others, at the discretion of the compiler; you should not depend on the inline specifier to cause a function to be inlined.

这些因素可能会干扰内联函数的使用,编译器还有其他可能的因素会自行决定是否使用内联函数,所以你不能只依靠inline指令就认为函数一定会被内联。


As with normal functions, there is no defined order of evaluation of the arguments to an inline function. In fact, it could be different from the order in which the arguments are evaluated when passed using normal function call protocol.

对于普通的函数,内联时输入参数将没有一个明确定义的顺序。实际上,参数传入的顺序可能与使用正常函数调用协议的传入顺序不同。


The /Ob compiler optimization option helps to determine whether inline function expansion actually occurs.

编译器优化指令/Ob帮助确认内联函数展开是否真正发生。(Visual Studio - Project Properties - C/C++ - Optimization - Inline Function Expansion)


/LTCG performs cross-module inlining regardless of whether it was requested in source code.

编译指令/LTCG则无论源代码中是否要求内联都执行跨模块的内联。

Example 1

// inline_keyword1.cpp// compile with: /cinline int max( int a , int b ) {   if( a > b )       return a;   return b;}

A class's member functions can be declared inline either by using the inline keyword or by placing the function definition within the class definition.

一个类成员函数实现内联的方式有两种:一种是使用inline关键字声明,一种是将函数的定义放置在类声明中(头文件内实现的函数)。

Example 2

// inline_keyword2.cpp// compile with: /EHsc /c#include <iostream>using namespace std;class MyClass {public:   void print() { cout << i << ' '; }   // Implicitly inlineprivate:   int i;};

Microsoft Specific

The __inline keyword is equivalent to inline.

__inline 关键字等同于inline关键字。


Even with __forceinline, the compiler cannot inline code in all circumstances. The compiler cannot inline a function if:

即使是__forceinline关键字,编译器也无法在所有的情况下都实现代码内联。编译器无法内联的函数如下:

  • The function or its caller is compiled with /Ob0 (the default option for debug builds).

    函数或者它的调用方使用指令/Ob0来编译(debug编译的默认方式)


  • The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other).

    函数和调用方使用不同方式的异常处理(一个使用C++异常处理,另一个使用结构化异常处理)


  • The function has a variable argument list.

    函数的参数列表是可变的。


  • The function uses inline assembly, unless compiled with /Og, /Ox, /O1, or /O2.

    函数使用了内联汇编,另如果使用编译指令/Og, /Ox, /O1, or /O2除外


  • The function is recursive and not accompanied by #pragma inline_recursion(on). With the pragma, recursive functions are inlined to a default depth of 16 calls. To reduce the inlining depth, use inline_depth pragma.

    函数是递归的并且没有使用编译指令#pragma inline_recursion(on). 对于该条指令,递归函数可以被默认内联到第16层,如果想要减少内联递归的层数,使用inline_depth编译指令


  • The function is virtual and is called virtually. Direct calls to virtual functions can be inlined.

    函数是虚函数并且被虚拟函数调用。直接对虚函数的调用是可以被内联的。


  • The program takes the address of the function and the call is made via the pointer to the function. Direct calls to functions that have had their address taken can be inlined.

    程序只得到了函数的地址,并根据函数指针来对函数进行调用。直接使用函数地址对函数进行调用是可以被内联的。


  • The function is also marked with the naked __declspec modifier.

    函数同时使用修饰符naked __declspec的。


If the compiler cannot inline a function declared with __forceinline, it generates a level 1 warning.

如果编译器不能将使用 __forceinline的函数内联,编译器将会生成一个警告。


Recursive functions can be substituted inline to a depth specified by the inline_depth pragma, up to a maximum of 16 calls. After that depth, recursive function calls are treated as calls to an instance of the function. The depth to which recursive functions are examined by the inline heuristic cannot exceed 16.

递归函数可以使用inline_depth编译指令指定内联的层数,最大到16层。超过16层以后,递归函数调用将采用函数调用的方式。内联函数试探检查的递归层数不能超过16。


The inline_recursionpragma controls the inline expansion of a function currently under expansion. See the Inline-Function Expansion (/Ob) compiler option for related information.

END Microsoft Specific

For more information on using the inline specifier, see:

  • Inline Class Member Functions

  • Inline Functions versus Macros

  • When to Use Inline Functions

  • Defining Inline C++ Functions with dllexport and dllimport

See Also

Reference

C++ Keywords
noinline
auto_inline
原创粉丝点击