c++ 内联的目的是什么?

我有一个discussionJohannes Schaub关于内联关键字.
代码有:

namespace ... {
    static void someFunction() {
        MYCLASS::GetInstance()->someFunction();
    }
};

他说:

Putting this as an inline function may
save code size in the executable

但是根据我的发现,herehere将不再需要,因为:

> [内联]仅在编译器的成本/效益分析显示为有利可图的情况下才会发生
主流C编译器如Microsoft Visual C和GCC支持一个选项,让编译器自动内联任何合适的功能,甚至没有标记为内联函数的函数.

然而约翰内斯却指出,明确指出它还有其他好处.不幸的是我不明白他们.例如,他表示,“内联”允许您在程序中多次定义该功能,我很难理解(并查找引用).

所以

是内联只是一个建议的编译器?
>当你有一个小功能(我猜1-4指令?)应该明确说明吗?
>在线写入还有什么其他好处?
>为了减少可执行文件的大小,是否需要进行内联处理,即使编译器(根据维基百科知道,不好的参考))应该自己找到这样的函数?

还有什么我失踪的吗?

Is inline just a recommendation for the compiler?

是.

7.1.2 Function specifiers

2 A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline
specifier indicates to the implementation that inline substitution of the function body at the point of call
is to be preferred to the usual function call mechanism. An implementation is not required to perform this
inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules
for inline functions defined by 7.1.2 shall still be respected.

例如从MSDN:

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.

注意:

3.2 One definition rule

3 […]An inline function shall be defined in every translation unit in which it is used.

4 An inline function shall be defined in every translation unit in which it is used and shall have exactly
the same definition in every case (3.2). [ Note: a call to the inline function may be encountered before its
definition appears in the translation unit. —end note ] If the definition of a function appears in a translation
unit before its first declaration as inline, the program is ill-formed. If a function with external linkage is
declared inline in one translation unit, it shall be declared inline in all translation units in which it appears;
no diagnostic is required.
An inline function with external linkage shall have the same address in all
translation units. A static local variable in an extern inline function always refers to the same object.
A string literal in the body of an extern inline function is the same object in different translation units.
[ Note: A string literal appearing in a default argument expression is not in the body of an inline function
merely because the expression is used in a function call from that inline function. —end note ] A type
defined within the body of an extern inline function is the same type in every translation unit.

[注:重点是我的]

TU基本上是一组标头加上导致目标文件的实现文件(.cpp).

Should it be explicitly stated when you have a small function (I
guess 1-4 instructions?)

绝对.为什么不帮助编译器帮助您生成更少的代码?通常,如果prolog / epilog部分比内联强制编译器产生更多的成本?但是,您必须绝对必须通过这个GOTW文章,然后开始内联:GotW #33: Inline

What other benefits are there with writing inline?

>命名空间也可以是内联的.请注意,默认情况下,类体内定义的成员函数是内联的.所以是隐式生成的特殊成员函数.
>函数模板不能在实现文件中定义(参见FAQ 35.12),除非您提供明确的实例化(对于模板使用的所有类型 – 通常为PITA IMO).请参阅Moving Templates Out of Header Files上的DDJ文章(如果您对于从标准中删除的导出关键字的其他文章感到奇怪).

Is it needed to state inline in order to reduce the executable file
size, even though the compiler
(according to wikipedia [I know, bad
reference]) should find such functions
itself?

再次,正如我所说,作为一个好的程序员,你应该可以帮助编译器.但是here’s C常见问题解答有关内联的内容.所以要小心并不是所有的编译器都做这种分析,所以你应该阅读他们的优化开关的文档.例如GCC做类似的事情:

You can also direct GCC to try to integrate all “simple enough” functions into their callers with the option -finline-functions.

大多数编译器允许您在一定程度上覆盖编译器的成本/效益比分析. MSDNGCC文档值得一读.

http://stackoverflow.com/questions/3647053/what-is-are-the-purposes-of-inline

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c++ 内联的目的是什么?