如果是"内联"无效的? (C语言) [英] When is "inline" ineffective? (in C)
问题描述
有些人喜欢使用在线
关键字 C
,并把大功能的标题。如果你认为这是无效的?我认为,有时候就算是讨厌,因为它是不寻常的。
Some people love using inline
keyword in C
, and put big functions in headers. When do you consider this to be ineffective? I consider it sometime even annoying, because it is unusual.
我的原则是,在线
应该用于访问非常频繁的小功能,或者为了有真正的类型检查。总之,我的口味引导我,但我不知道该如何解释最佳的原因在线
是大功能不是那么有用。
My principle is that inline
should be used for small functions accessed very frequently, or in order to have real type checking. Anyhow, my taste guide me, but I am not sure how to explain best the reasons why inline
is not so useful for big functions.
在这个问题人建议编译器可以做得更好在猜测做正确的事情。这也是我的假设。当我尝试使用这种说法,回复的人不与功能从不同的对象来工作。好了,我不知道(例如,使用GCC)。
In this question people suggest that the compiler can do a better job at guessing the right thing to do. That was also my assumption. When I try to use this argument, people reply it does not work with functions coming from different objects. Well, I don't know (for example, using GCC).
谢谢您的回答!
推荐答案
在线
做了两件事:
- 为您提供了从一个定义规则(见下文)的豁免。这的总是的应用。
- 给人的编译器的提示,以避免函数调用。编译器可以自由地忽略这一点。
1是非常有用的(例如把定义标题,如果短),即使#2是禁用的。
#1 Can be very useful (e.g. put definition in header if short) even if #2 is disabled.
在实践中的编译器经常做的工作出了什么内联自己一个更好的工作(特别是如果配置文件导引优化可用)。
In practice compilers often do a better job of working out what to inline themselves (especially if profile guided optimisation is available).
以上两点都来自ISO / ANSI标准(ISO / IEC 9899:1999(E),俗称C99)遵循
The two points above both follow from the ISO/ANSI standard (ISO/IEC 9899:1999(E), commonly known as "C99").
在§6.9外部定义,第5款:
In §6.9 "External Definition", paragraph 5:
这是外部定义的是,这也是一个函数(不是内联函数等)或对象的定义的外部声明。如果有外部链接声明的标识符在离pression使用(不是作为sizeof操作符,其结果是一个整型常量的操作数的部分除外),地方整个节目须为的只有一个外部定义标识符;否则,应不超过一个。
An external definition is an external declaration that is also a definition of a function (other than an inline definition) or an object. If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one.
而在C ++中equalivalent定义是显式指定的一个定义规则(ODR)它有异曲同工之妙。外部(即不是静态,因而地方到一个翻译单位 - 通常是一个源文件)只能定义一次只的除非的是一个函数的和的内联。
While the equalivalent definition in C++ is explictly named the One Definition Rule (ODR) it serves the same purpose. Externals (i.e. not "static", and thus local to a single Translation Unit -- typically a single source file) can only be defined once only unless it is a function and inline.
在§6.7.4,功能说明符,inline关键字定义:
In §6.7.4, "Function Specifiers", the inline keyword is defined:
制作功能的内联函数建议调用该函数作为
越快越好。 [118] 这样的建议是其有效程度是
实现定义的。
Making a function an inline function suggests that calls to the function be as fast as possible.[118] The extent to which such suggestions are effective is implementation-defined.
和脚注(非规范),但提供了澄清:
And footnote (non-normative), but provides clarification:
通过使用,例如,一个替代通常的函数调用机制,如''联替代''。内联替换无法文本替换,也没有建立一个新的功能。因此,例如,函数的体内使用的宏扩展使用它不得不在显示的功能体的点的定义,而不是其中函数被调用;和标识符指的声明的范围在身体发生。同样地,功能有一个单一的地址,而不管行内定义的数目的发生在除了外部定义
By using, for example, an alternative to the usual function call mechanism, such as ‘‘inline substitution’’. Inline substitution is not textual substitution, nor does it create a new function. Therefore, for example, the expansion of a macro used within the body of the function uses the definition it had at the point the function body appears, and not where the function is called; and identifiers refer to the declarations in scope where the body occurs. Likewise, the function has a single address, regardless of the number of inline definitions that occur in addition to the external definition.
摘要:什么C和C的大多数用户++从内嵌想到的是不是他们得到的。其明显的主要目的,以避免功能调用的开销,完全是可选的。但允许单独的编译,单一的定义放宽是必需的。
Summary: what most users of C and C++ expect from inline is not what they get. Its apparent primary purpose, to avoid functional call overhead, is completely optional. But to allow separate compilation, a relaxation of single definition is required.
(从标准引号的所有重点。)
(All emphasis in the quotes from the standard.)
编辑2:一些注意事项:
EDIT 2: A few notes:
- 有外部内联函数的各种限制。你不能在函数中的静态变量,你不能引用静态TU范围的对象/功能。
- 刚刚看到这对VC ++的<一个href=\"http://blogs.msdn.com/vcblog/archive/2009/02/24/quick-tips-on-using-whole-program-optimization.aspx\">whole程序优化的,这是一个编译器操作的其自己的行内的事情,而不是提交一个例子。
- There are various restrictions on external inline functions. You cannot have a static variable in the function, and you cannot reference static TU scope objects/functions.
- Just seen this on VC++'s "whole program optimisation", which is an example of a compiler doing its own inline thing, rather than the author.
这篇关于如果是&QUOT;内联&QUOT;无效的? (C语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!