7.6. Inline Functions7.6. 内联函数Recall the function we wrote on page 248 that returned a reference to the shorter of its two string parameters: 回顾在第 7.3.2 节编写的那个返回两个 string 形参中较短的字符串的函数: // find longer of two strings const string &shorterString(const string &s1, const string &s2) { return s1.size() < s2.size() ? s1 : s2; } The benefits of defining a function for such a small operation include: 为这样的小操作定义一个函数的好处是:
There is, however, one potential drawback to making shorterString a function: Calling a function is slower than evaluating the equivalent expression. On most machines, a function call does a lot of work: registers are saved before the call and restored after the return; the arguments are copied; and the program branches to a new location. 但是,将 shorterString 写成函数有一个潜在的缺点:调用函数比求解等价表达式要慢得多。在大多数的机器上,调用函数都要做很多工作;调用前要先保存寄存器,并在返回时恢复;复制实参;程序还必须转向一个新位置执行。 inline Functions Avoid Function Call Overheadinline 函数避免函数调用的开销A function specified as inline (usually) is expanded "in line" at each point in the program in which it is invoked. Assuming we made shorterString an inline function, then this call 将函数指定为 inline 函数,(通常)就是将它在程序中每个调用点上“内联地”展开。假设我们将 shorterString 定义为内联函数,则调用: cout << shorterString(s1, s2) << endl; would be expanded during compilation into something like 在编译时将展开为: cout << (s1.size() < s2.size() ? s1 : s2) << endl; The run-time overhead of making shorterString a function is thus removed. 从而消除了把 shorterString 写成函数的额外执行开销。 We can define shorterString as an inline function by specifying the keyword inline before the function's return type: 从而消除了把 shorterString 写成函数的额外执行开销。 // inline version: find longer of two strings inline const string & shorterString(const string &s1, const string &s2) { return s1.size() < s2.size() ? s1 : s2; }
In general, the inline mechanism is meant to optimize small, straight-line functions that are called frequently. Many compilers will not inline a recursive function. A 1,200-line function is also not likely to be explanded inline. 一般来说,内联机制适用于优化小的、只有几行的而且经常被调用的函数。大多数的编译器都不支持递归函数的内联。一个 1200 行的函数也不太可能在调用点内联展开。 Put inline Functions in Header Files把 inline 函数放入头文件
To expand the code of an inline function at the point of call, the compiler must have access to the function definition. The function prototype is insufficient. inline 函数的定义对编译器而言必须是可见的,以便编译器能够在调用点内联展开该函数的代码。此时,仅有函数原型是不够的。 An inline function may be defined more than once in a program as long as the definition appears only once in a given source file and the definition is exactly the same in each source file. By putting inline functions in headers, we ensure that the same definition is used whenever the function is called and that the compiler has the function definition available at the point of call. inline 函数可能要在程序中定义不止一次,只要 inline 函数的定义在某个源文件中只出现一次,而且在所有源文件中,其定义必须是完全相同的。把 inline 函数的定义放在头文件中,可以确保在调用函数时所使用的定义是相同的,并且保证在调用点该函数的定义对编译器可见。
|