编译器在什么阶段执行内联?

发布于 2024-09-06 22:26:46 字数 193 浏览 1 评论 0原文

这是一个关于c++中内联函数的小问题。

在 C++ 编译的哪个阶段,内联函数实际上在调用时内联?

这基本上是如何运作的。

假设在程序员在函数前面使用 inline 关键字请求后,编译器决定某个特定函数必须内联,那么编译器何时为程序员执行此操作。我的意思是在编译的哪个阶段。

是在预处理阶段像c宏一样被扩展吗?

here is a small question about inline functions in c++.

At what stage of the compilation in C++ are the inline functions actually inlined at the call?

how does that basically work.

lets say if the compiler has decided that a particualr function has to be inline after the programmer has requested with an inline keyword in front of the function ,when does the compiler does that for the programmer .i mean at what stage of the compilation.

is it at the preprocessing stage like in c macros are expanded?

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

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

发布评论

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

评论(1

萝莉病 2024-09-13 22:26:46

它会因编译器而异。并且某些编译器中的某些阶段在其他编译器中没有对应的阶段。所以你的问题其实并没有明确的答案。

但通常它是在创建函数的解析树之后、实际生成代码或完成许多优化之前完成的。这是执行此操作的最佳位置,因为您希望优化器可以使用最大数量的信息。

像预处理器宏扩展一样这样做通常还为时过早。这样,编译器就没有足够的信息来进行适当的类型检查,并且也更容易犯错误,导致多次发生副作用等等。

GMan 在评论中提供了一个出色的维基百科链接,其中详细介绍了函数内联过程 比我在这里做的。我的答案基本上是正确的,但存在很多变化,甚至比我想象的还要多。

It will vary by compiler. And some stages in some compilers will have no corresponding stages in other compilers. So your question doesn't really have a definite answer.

But generally it's done after the parse tree for the function is created, but before code is actually generated or many optimizations are done. This is the most optimum place to do it because you want the maximum amount of information available for optimizer to work with.

Doing it like a preprocessor macro expansion would be generally too early. The compiler doesn't then have enough information to do the appropriate type checking, and it's easier also to make mistakes that cause side effects to happen more than once and so on.

And GMan provided an excellent Wikipedia link in a comment that goes into much more detail about the function inlining process than I do here. My answer is generally true, but there is a LOT of variation, even more than I thought there was.

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