天天看點

inline, __inline, __forceinline MSDN翻譯 inline, __inline, __forceinline

inline, __inline, __forceinline

Visual Studio 2010 Other Versions

inline, __inline, __forceinline MSDN翻譯 inline, __inline, __forceinline

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
      
inline, __inline, __forceinline MSDN翻譯 inline, __inline, __forceinline

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: /c
inline 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 inline
private:
   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
inline, __inline, __forceinline MSDN翻譯 inline, __inline, __forceinline

See Also

Reference

C++ Keywords noinline auto_inline