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

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

问题描述

我有一个讨论 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

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

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


    <如果编译器的成本/效益分析显示它是有利可图,则
  • 主流C ++编译器如Microsoft Visual C ++和GCC支持

  • [Inline] only occurs if the compiler's cost/benefit analysis show it to be profitable
  • Mainstream C++ compilers like Microsoft Visual C++ and GCC support an option that lets the compilers automatically inline any suitable function, even those not marked as inline functions.

Johannes然而声明了一个选项,允许编译器自动内联任何合适的函数,即使没有标记为内联函数。有明确指定它的其他好处。不幸的是我不明白他们。例如,他说,和inline允许你在程序中多次定义函数。,我很难理解(并找到引用)。

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. 是否需要在 inline 中写入 inline

  4. 为了减少可执行文件大小,即使编译器(根据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 there anything else I am missing?

推荐答案


编译器?

Is inline just a recommendation for the compiler?

是的。


7.1.2函式说明符

strong> 2 带有内联说明符的函数声明(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.

注意: / p>

Note though:


3.2一个定义规则

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

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

4 内联函数应在每个使用它的翻译单元中定义,并且在每种情况下(3.2)都有正确的
相同的定义。 [注意:在
定义出现在翻译单元中之前,可能会遇到对内联函数的调用。 - end note]如果函数的定义在第一次声明为inline之前出现在翻译
单元中,则程序是不成形的。 如果带有外部链接的函数
在一个翻译单元中声明为inline,则它应在其出现的所有翻译单元中声明为inline;
无需诊断。
具有外部链接的内联函数在所有
翻译单元中应具有相同的地址。 extern内联函数中的静态局部变量总是引用同一个对象。
外部内联函数体中的字符串文字是不同翻译单元中的同一个对象。
[注意:出现在默认参数表达式中的字符串文字不在内联函数体(
)中,因为该表达式用于来自内联函数的函数调用。 - end note]在extern内联函数体内定义的
类型在每个翻译单元中都是相同的类型。

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.


当你有一个小函数4个指令?)

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

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

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


/ p>

What other benefits are there with writing inline?




  • 命名空间 code> 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.)


      是否需要内联以减少可执行文件
      的大小,即使编译器
      (根据wikipedia [我知道,bad
      reference])应该找到这样的函数
      本身?

      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 ++常见问题提供 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尝试将所有简单功能集成到调用者 - 线功能。

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

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

      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天全站免登陆