C++ 内联函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5971736/
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
c++ inline function?
提问by Pwnna
Why should i do something like this:
为什么我应该做这样的事情:
inline double square (double x) { return x*x;}
instead of
代替
double square (double x) { return x*x;}
Is there a difference?
有区别吗?
回答by Greg Hewgill
The former (using inline
) allows you to put that function in a header file, where it can be included in multiple source files. Using inline
makes the identifier in file scope, much like declaring it static
. Without using inline
, you would get a multiple symbol definition error from the linker.
前者(使用inline
)允许您将该函数放在一个头文件中,它可以包含在多个源文件中。使用inline
使标识符在文件范围内,就像声明它一样static
。如果不使用inline
,您将收到来自链接器的多符号定义错误。
Of course, this is in addition to the hint to the compiler that the function should be compiled inlineinto where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline
hint.
当然,这是对编译器的额外提示,即函数应该被内联编译到使用它的地方(避免函数调用开销)。编译器不需要根据inline
提示采取行动。
回答by Erik Nedwidek
Yes there is a difference. https://isocpp.org/wiki/faq/inline-functions.
是,有一点不同。https://isocpp.org/wiki/faq/inline-functions。
When you specify that a function is inline you are causing the compiler to put the code of the method in where ever it is being called.
当您指定一个函数是内联的时,您会导致编译器将该方法的代码放在调用它的地方。
void myfunc() {
square(2);
}
is identical to
等同于
void myfunc() {
2 * 2;
}
Calling a function is good for code clarity, but when that function is called the local state has to be pushed to the stack, a new local state is setup for the method, and when it is done the previous state needs to be popped. That is a lot of overhead.
调用一个函数有利于代码的清晰,但是当调用该函数时,必须将本地状态压入堆栈,为该方法设置一个新的本地状态,完成后需要弹出先前的状态。这是很多开销。
Now if you up your optimization level, the compiler will make decisions like unrolling loops or inlining functions. The compiler is still free to ignore the inline statement.
现在,如果您提高优化级别,编译器将做出诸如展开循环或内联函数之类的决定。编译器仍然可以随意忽略内联语句。
回答by Ben Hymanson
On a modern compiler there is likely not much difference. It may be inlined without the inline
and it may notbe inlined withthe inline
.
在现代编译器上可能没有太大区别。它可以不被内联inline
,它可能不会被内联用的inline
。
回答by johannesMatevosyan
From Wikipedia: Inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.
来自维基百科:内联函数是要求编译器执行内联扩展的函数。换句话说,程序员要求编译器在调用函数的每个地方插入完整的函数体,而不是在定义函数的地方生成代码来调用函数。编译器没有义务尊重这一要求。
回答by Ian Fleeton
The inline function, if the compiler complies, will include the inline function in the code in which it was called as if no function was called (as though you had put the logic in the calling function) and avoid the function call overhead.
内联函数(如果编译器符合要求)将在调用它的代码中包含内联函数,就好像没有调用任何函数一样(就好像您已将逻辑放在调用函数中一样)并避免函数调用开销。
回答by Gio Borje
inline
works well with the concept of procedural abstraction:
inline
与过程抽象的概念配合得很好:
inline double square (double x) { return x*x;}
int squareTwice(double x) {
double first = square(x);
double second = square(x);
return first * second;
}
The above is fundamentally similar to the following:
上述内容与以下内容基本相似:
int squareTwice(double x) {
double first = x*x;
double second = x*x;
return first * second;
}
This happens because when the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream; therefore, it may be easier to procedurally abstract the second example to the first example.
发生这种情况是因为当编译器内联扩展一个函数调用时,该函数的代码被插入到调用者的代码流中;因此,在程序上将第二个示例抽象为第一个示例可能更容易。
Procedural abstraction makes it possible to break up a routine into smaller subroutines that are much easier to read (although this can be a style choice).
过程抽象使得将例程分解为更容易阅读的更小的子例程成为可能(尽管这可能是一种风格选择)。