编译器是否决定何时内联我的函数(在 C++ 中)?

发布于 2024-07-29 07:11:02 字数 383 浏览 7 评论 0原文

我知道您可以使用 inline 关键字,或者只是将一个方法放在类声明中(例如短构造函数或 getter 方法),但是编译器是否会最终决定何时内联我的方法?

例如:

inline void Foo::vLongBar()
{
   //several function calls and lines of code
}

如果编译器认为内联声明会使我的代码效率低下,它会忽略它吗?

作为一个附带问题,如果我在类外部声明了一个 getter 方法,如下所示:

void Foo::bar() { std::cout << "baz"; }

编译器会在幕后内联此方法吗?

I understand you can use the inline keyword or just put a method in a class declaration ala short ctor or a getter method, but does the compiler make the final decision on when to inline my methods?

For instance:

inline void Foo::vLongBar()
{
   //several function calls and lines of code
}

Will the compiler ignore my inline declaration if it thinks it will make my code inefficient?

As a side issue, if I have a getter method declared outside my class like this:

void Foo::bar() { std::cout << "baz"; }

Will the compiler inline this under the covers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

奶茶白久 2024-08-05 07:11:02

是的,是否内联代码的最终决定取决于 C++ 编译器。 inline 关键字是建议,而不是要求。

以下是有关 Microsoft C++ 编译器如何处理此决定的一些详细信息

Yes, the final decision of whether or not to inline your code lies in the C++ compiler. The inline keyword is a suggestion, not a requirement.

Here are some details as to how this decision is processed in the Microsoft C++ Compiler

多情癖 2024-08-05 07:11:02

归根结底,函数是否内联完全取决于
编译器。 通常,函数的流程越复杂,编译器内联它的可能性就越小。 有些函数,例如递归函数,根本无法内联。

不内联函数的主要原因是它会大大增加代码的整体大小,从而阻止 iot 被保存在处理器的缓存中。 这实际上是一种悲观,而不是优化。

至于让程序员决定搬起石头砸自己的脚,还是在其他地方,您可以自己内联函数 - 在函数的调用位置编写本来应该放入函数中的代码。

Whether or not a fiunction is inlined is, at the end of the day, entirely up to
the compiler. Typically, the more complex a function is in terms of flow, the less likely the compiler is to inline it. and some functions, such as recursive ones, simply cannot be inlined.

The major reason for not inlining a function is that it would greatly increase the overall size of the code, preventing iot from being held in the processor's cache. This would in fact be a pessimisation, rather than an optimisation.

As to letting the programmer decide to shoot himself in the foot, or elsewhere, you can inline the function yourself - write the code that would have gone in the function at what would have been the function's call site.

深者入戏 2024-08-05 07:11:02

正如其他人所指出的,inline 关键字只是建议编译器内联代码。 由于编译器通常会内联尚未标记为 inline 的代码,而不是已标记为内联的代码,因此该关键字似乎与 register 或 (pre-C ++0x) 自动

然而,inline 关键字还有另外一件事:它将函数的链接外部(函数的默认设置)更改为内联。 内联链接允许每个编译单元包含其自己的目标代码副本,并让链接器从最终可执行文件中删除冗余副本。 如果这让您想起模板,是的,模板也使用内联链接。

As others have noted, the inline keyword is merely a suggestion to the compiler to inline the code. Since the compiler will routinely inline code that hasn't been marked with inline, and not inline code that has, the keyword seems just as redundant as register or (pre-C++0x) auto.

However, there is one other thing that the inline keyword effects: It changes the linkage of the function from external (the default for functions) to inline. Inline linkage allows each compilation unit to contain it's own copy of the object code, and has the linker remove redundant copies from the final executable. If that reminds you of templates, yes, templates also use inline linkage.

浮生面具三千个 2024-08-05 07:11:02

正如许多人已经发布的那样,最终的决定始终取决于编译器,即使您可以给出诸如forceinline之类的明确提示。
部分原因是内联不是一个自动的“更快”开关。 过多的内联会使您的代码变得更大,并且可能会干扰其他优化。 请参阅有关内联函数和性能的 C++ FAQ Lite。

As many have already posted, the final decision is always up to the compiler, even if you can give firm hints such as forceinline.

Part of the rationale is that inlining is not an automatic "go faster" switch. Too much inlining can make your code much larger, and may interfere with other optimizations. See The C++ FAQ Lite about inline functions and performance.

可遇━不可求 2024-08-05 07:11:02

是的,编译器有最终决定权。 在 VS 中,您甚至可以将递归函数内联到指定深度;)

#pragma inline_depth( [0... 255] )

Yes, the compiler has the final decision. In VS you can even inline recursive functions into specified depth ;)

#pragma inline_depth( [0... 255] )
奢欲 2024-08-05 07:11:02

只是添加我的 5 美分...

我发现这篇关于内联的 本周大师 文章很有用。

据我记得我在某处读到,当链接器链接目标文件并发现链接的代码可以内联时,即使链接器也可能会内联。

Just to add my 5 cents ...

I found this Guru of Week article about inlining very useful.

As far as I remember I read somewhere that even a linker might do inlining, when it links the object files and finds that the code being linked can be inlined.

似梦非梦 2024-08-05 07:11:02

作为一个附带问题,如果我在类之外声明了一个 getter 方法,如下所示:

void Foo::bar() { std::cout << "baz"; }

编译器会在幕后内联它吗?

这取决于。 它可以用于同一翻译单元(.cpp 文件及其所有 #included 定义)中的所有调用者。 但它仍然必须编译非内联版本,因为翻译单元之外可能有该函数的调用者。 您可能会在高优化级别上看到这一点(如果您的编译器实际上可以做到)。 (特别是:比较当您在一个 .cpp 中 #include 所有 .cpp 文件与典型布局时发生的情况。通过一个翻译单元中的所有定义,这种内联的机会显着增加。)

As a side issue, if I have a getter method declared outside my class like this:

void Foo::bar() { std::cout << "baz"; }

Will the compiler inline this under the covers?

It depends. It can for all callers in the same translation unit (the .cpp file and all of its #included definitions). But it still has to compile a non-inlined version because there may be callers of that function outside the translation unit. You can potentially see this at work (if your compiler actually can do it) at high optimization levels. (In particular: compare what happens when you #include all your .cpp files in one .cpp vs. the typical layout. With all definitions in one translation unit the opportunities for such inlining increase dramatically.)

榆西 2024-08-05 07:11:02

据我所知,如果编译器找到像 for、while 等循环,它会自动将您声明为内联(或在类声明中编写)的函数设为非内联。
这是编译器在内联函数中拥有最后发言权的一个例子。

As per my knowledge, compiler will automatically make a function you declared inline (or wrote inside a class declaration) non -inline if it finds a loop like for, while etc.
This is one example where compiler has the last say in inline functions.

巨坚强 2024-08-05 07:11:02

如果您确实、肯定、绝对需要内联代码,那么宏总是存在的。 C 多年来一直支持这些,因为它们只是编译前的文本替换,所以它们确实内联了您编写的任何内容。

这就是为什么“inline”关键字(甚至在某些情况下,强制变体)可以没有强制它的标准方法 - 您总是可以只编写一个宏。

也就是说,inline 关键字通常更好,因为编译器通常知道使函数内联是否有意义,并且因为 inline 可以与编译器优化的其余部分交互。

If you really, positively, absolutely, without fail NEED to inline the code, there is always the macro. C has supported these for years and because they are just text replacement prior to compilation, they really, truly, inline whatever you write.

That's why the 'inline' keyword (and even, in some cases, the forced variants) can afford to not have a standard way of forcing it - you can always just write a macro.

That said, the inline keyword is often better, because the compiler quite often knows if making a function inline makes sense or not, and because the inline can interact with the rest of the compiler optimizations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文