编译器什么时候内联函数? C ++ [英] When does the compiler do inline a function? C++

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

问题描述

现在已经阅读了许多aricels并且没有得到它,也没有找到汇编。我完全糊涂了!

这里我发现的规则:

是正确的????



A)内联键现在在C ++ 11中已经过时,编译器总是决定自己的

Have read through many aricels now and do not get it, nor found a compendium. Im totally confused!
Here the rules I found out:
is that correct????

A) inline keyord now is obsolete in C++11, the Compiler always decides on ist own

int add(int x) { return x+1; } 
inline int add(int x) { return x+1; }



也这样做!



B)函数Body必须放在类声明中如果不是类成员,则在其使用的cpp文件中。

.h:


do the same!

B) The function Body has to be placed in the class declaration OR if not a class member, in the cpp file where its used.
.h:

virtual int add(int x);

.cpp

int myclass::add(int x) { return x+1 }



永远不会内联???



Visual Studio 2013,C ++和一些MFC



(我们有许多转储函数不执行任何操作或仅在我们的C ++代码中返回true / false)



我尝试了什么:



已阅读了许多关键词,网络论坛,但没有得到它。

和?有没有办法找出编译器的功能?



感谢Folks!


never will be inlined ???

Visual Studio 2013, C++ and some MFC

(We have many dump functions that do nothing or only return true/false in our C++ Code)

What I have tried:

have read through many articels, web Forums but did´n get it.
And? Is there a way to find out what the Compiler did?

thanks Folks!

推荐答案

它依赖于编译器,也受代码生成/优化选项的影响。所以你必须检查编译器特定的文档。对于Microsoft编译器,请参阅内联函数(C ++) [ ^ ]。



我知道检查函数是否已内联的唯一可靠方法是检查生成的代码(例如,使用 / FA,/ Fa(列表文件) [ ^ ]选项或检查符号表。)
It is compiler dependant and also affected by code generation / optimisation options. So you have to check the compiler specific documentation. For the Microsoft compiler see Inline Functions (C++)[^].

The only reliable ways I know to check if a function has been inlined or not is inspecting the generated code (e.g. by using the assembly output using the /FA, /Fa (Listing File)[^] option or inspecting the symbol table).


编译器选择。例如,请参阅内联说明符 - cppreference.com [ ^ ]。
The compiler chooses. See, for instance inline specifier - cppreference.com[^].


A)内联是代码优化,现代编译器在代码优化方面已经比程序员*更好。因此,将决策权交给编译器几乎总是最佳选择。情况并非总是如此,因此,在过去,编译器构建器以内联关键字的形式向程序员请求提示是有意义的。然而,C ++ 11认识到这不再合理。



B)虚拟函数不能<编译时code>内联 d,因为必须在运行时决定调用哪个函数。**



PS:

*:除非你是实现编译器优化算法的程序员

**:许多编译器如果函数不会内联甚至非虚函数实现放在一个单独的文件中。原因是实现文件通常对任何其他编译单元都不可见,并且每次编译器找到对该函数的调用时都需要花费很多额外的工作来查找实现代码。现代编译器仍然可以这样做,但不能保证。
A) Inlining is code optimization, and modern compilers are already way better at code optimization than programmers*. Because of that, leaving the decision to the compiler is almost always the best option. This wasn't always the case, so, in the past, it made sense for compiler builders to ask programmers for a hint, in the form of the inline keyword. C++11 however recognizes that this is no longer sensible.

B) virtual functions can not be inlined at compile-time, because which function has to be called has to be decided at run-time.**

P.S.:
*: unless you are the programmer who implemented the compiler optimization algorithms
**: many compilers will not inline even non-virtual functions if the function implementation is placed in a separate file. The reason is that the implementation file is typically not visible to any other compilation unit, and it would take a lot of extra effort to find the implementation code every time the compiler finds a call to that function. Modern compilers still may do that, but there's no guarantee.


这篇关于编译器什么时候内联函数? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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