内联的目的是什么? [英] what is/are the purpose(s) of inline?

查看:33
本文介绍了内联的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与 Johannes Schaub 关于关键字 inline.那里的代码是这样的:

I had a discussion with Johannes Schaub regarding the keyword inline. The code there was this:

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

他说:

把它作为一个内联函数可能在可执行文件中保存代码大小

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

但根据我的发现 here这里不需要,因为:

But according to my findings here and here it wouldn't be needed, since:

  • [内联] 仅在编译器的成本/收益分析表明它是有利可图的情况下才会发生
  • Microsoft Visual C++ 和 GCC 等主流 C++ 编译器支持一个选项,允许编译器自动内联任何合适的函数,即使是那些未标记为内联函数的函数.

然而,Johannes 指出,明确指定它还有其他好处.不幸的是,我不明白他们.例如,他说并且内联"允许您在程序中多次定义函数.,我很难理解(并找到参考).

Johannes however states that there are other benefits of explicitly specifying it. Unfortunately I do not understand them. For instance, he stated that And "inline" allows you to define the function multiple times in the program., which I am having a hard time understanding (and finding references to).

所以

  1. inline 只是对编译器的建议吗?
  2. 当你有一个小功能时是否应该明确说明(我猜是1-4条指令?)
  3. 编写内联还有哪些其他好处?
  4. 是否需要声明 inline 以减小可执行文件的大小,即使编译器(根据 wikipedia [我知道,错误参考])应该自己找到这些函数?
  1. Is inline just a recommendation for the compiler?
  2. Should it be explicitly stated when you have a small function (I guess 1-4 instructions?)
  3. What other benefits are there with writing inline?
  4. 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?

我还有什么遗漏的吗?

推荐答案

内联只是对编译器的建议吗?

Is inline just a recommendation for the compiler?

是的.

7.1.2 函数说明符

2 带有 inline 说明符的函数声明(8.3.5、9.3、11.4)声明了一个内联函数.内联说明符向实现指示函数体在调用点的内联替换优于通常的函数调用机制.执行此操作不需要实现调用点的内联替换;但是,即使省略了这个内联替换,其他规则对于 7.1.2 定义的内联函数,仍应遵守.

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:

编译器将内联扩展选项和关键字视为建议.不能保证函数会被内联.即使使用 __forceinline 关键字,您也不能强制编译器内联特定函数.使用/clr 编译时,如果函数应用了安全属性,编译器将不会内联函数.

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 一个定义规则

3 [...]内联函数应在使用它的每个翻译单元中定义.

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

4 内联函数应在使用它的每个翻译单元中定义,并且应具有在每种情况下都具有相同的定义(3.2).[注意:对内联函数的调用可能会在其之前遇到定义出现在翻译单元中.—尾注] 如果函数的定义出现在翻译中单元在其第一次声明为内联之前,该程序格式错误.如果一个具有外部链接的函数是在一个翻译单元中声明为 inline,则应在其出现的所有翻译单元中声明为 inline;不需要诊断.具有外部链接的内联函数应具有相同的地址翻译单位.外部内联函数中的静态局部变量始终引用同一个对象.外部内联函数主体中的字符串文字是不同翻译单元中的相同对象.[注意:出现在默认参数表达式中的字符串文字不在内联函数的主体中仅仅因为该表达式用于该内联函数的函数调用中.——尾注] A型在外部内联函数的主体中定义的类型在每个翻译单元中都是相同的.

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),该文件通向一个目标文件.

A TU is basically a set of headers plus an implementation file (.cpp) which leads to an object file.

当你有一个小功能时是否应该明确说明(我猜1-4条指令?)

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

当然.为什么不帮助编译器帮助您生成更少的代码呢?通常,如果 prolog/epilog 部分比让它内联强制编译器生成它们产生更多的成本?但是您必须,绝对必须在开始使用内联之前阅读这篇 GOTW 文章:GotW #33:内联

Absolutely. Why not help the compiler help you generate less code? Usually, if the prolog/epilog part incurs more cost than having it inline force the compiler to generate them? But you must, absolutely must go through this GOTW article before getting started with inlining: GotW #33: Inline

内联写作还有什么其他好处?

What other benefits are there with writing inline?

  • namespace 也可以是 inline.请注意,类主体本身中定义的成员函数默认情况下是内联的.隐式生成的特殊成员函数也是如此.

    • namespaces can be inline too. Note that member functions defined in the class body itself are inline by default. So are implicitly generated special member functions.

      函数模板不能在实现文件中定义(见 FAQ 35.12) 当然,除非您提供明确的实例化(对于使用模板的所有类型——通常是 PITA IMO).请参阅将模板移出头文件上的 DDJ 文章(如果您是阅读另一篇关于从标准中删除的 export 关键字的文章时感觉很奇怪.)

      Function templates cannot be defined in an implementation file (see FAQ 35.12) unless of course you provide a explicit instantiations (for all types for which the template is used -- generally a PITA IMO). See the DDJ article on Moving Templates Out of Header Files (If you are feeling weird read on this other article on the export keyword which was dropped from the standard.)

      是否需要声明 inline 以减少可执行文件大小,即使编译器(根据维基百科 [我知道,不好参考])应该找到这样的功能自己?

      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?

      同样,正如我所说,作为一名优秀的程序员,你应该尽可能地帮助编译器.但是这里是 C++ FAQ 的内容提供关于inline.所以要小心.并非所有编译器都会进行此类分析,因此您应该阅读有关其优化开关的文档.例如:GCC 做了类似的事情:

      Again, as I said, as a good programmer, you should, when you can, help the compiler. But here's what the C++ FAQ has to offer about inline. So be wary. Not all compilers do this sort of analysis so you should read the documentation on their optimization switches. E.g: GCC does something similar:

      您还可以指示 GCC 尝试使用选项 -finline-functions 将所有足够简单"的函数集成到它们的调用者中.

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

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

      Most compilers allow you to override the compiler's cost/benefit ratio analysis to some extent. The MSDN and GCC documentation is worth reading.

      这篇关于内联的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆