内联函数有什么要求?

发布于 2024-09-12 23:30:30 字数 84 浏览 2 评论 0原文

函数需要什么才能在 C++ 中内联执行? 是否存在函数无法内联的情况,或者任何函数都可以内联,并且程序员有责任根据运行时和编译时的考虑来决定如何定义函数?

What are the requirement for a function so it could be executed inline in C++?
Is there a case that a function can't be inlined, or can any function be inlined and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

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

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

发布评论

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

评论(5

把回忆走一遍 2024-09-19 23:30:30

这取决于你的意思。如果你的意思是,什么时候可以内联扩展函数,删除函数调用,那么这实际上取决于编译器。它可以内联几乎任何函数,并拒绝内联几乎任何您要求的函数。可能不会内联的函数包括递归函数以及已获取函数地址的函数。总而言之,还是别想那么多为好。

inline 关键字的主要用途不是请求内联,而是指示该函数可以 #included 到多个翻译单元中,而不会导致多个定义错误。

It depends what you mean. If you mean, when can the function be expanded in-line, removing a function call, then that is really up to the compiler. It may inline almost any function, and refuse to inline almost any function that you ask it to. Functions that probably won't be inlined include recursive ones, and ones where you have taken the function's address. On the whole, it's better not to think about this much.

The main use for the inline keyword is not to request inlining, but to indicate that the function may be #included in multiple translation units without causing multiple definition errors.

折戟 2024-09-19 23:30:30

申请的条件是什么
函数因此可以被执行
在 C++ 中内联?

它需要在调用它的每个位置进行定义(通常这是通过将其放入 .h 中来完成的)。

有没有函数不能的情况
内联吗?

我相信,不是就语言标准而言,尽管每个编译器当然可以并且将会实现自己的一些限制。

或者任何函数都可以是内联的
是的责任
程序员决定如何定义
函数,基于运行时间和
编译时考虑因素?

inline 关键字只是程序员向编译器发出的一个提示,表明程序员确实希望内联该函数(大概程序员已经发现在“hot”中调用一个小函数会产生大量的调用开销)如探查器所示的循环 - 或者,该函数非常小,几乎与调用代码一样小;-) - 或者,内联该函数允许“跨函数边界优化”,而特定编译器无法发现或否则无法执行——等等)。

编译器可以自由地忽略该提示,就像它可以自由地忽略变量存储类的旧 register 提示一样(我相信现在大多数优化的 C++ 编译器确实会忽略 register 但很少)忽略inline)——IOW,编译器可以自由地内联对函数的所有或部分调用,无论该函数是否声明为inline

(当然,当它们通过指向在某些点隐藏并在其他点使用的函数的显式指针完成时,或者当函数的地址作为参数传递给某个其他函数时,它不会“内联调用” -但这可能会影响特定调用的内联,而不一定会影响以不同方式完成的对同一函数的其他调用)。

“以防万一”你的编译器非常认真地接受你的 inline 提示,通常值得测量有或没有 inline 的代码大小和速度(除非你的编译器提供了选项/标志为此,只需使用 #define inline 就肯定会“禁用”inline 的效果,从而允许进行此类测量)。但是,如果您要跨多个编译器部署代码,尤其是。对于多种架构,请注意,考虑到 CPU 架构中编译器优化策略的差异,在一个平台上产生的积极影响可能最终在另一个平台上产生反作用。

what are the requirement for a
function so it could be executed
inline in c++?

It needs to be defined at every spot in which it's called (typically this is done by putting it in a .h).

is there a case that a function can't
be inline?

Not in terms of the language standard, I believe, although of course each compiler can and will implement some restrictions of its own.

or any function can be inline and it
is the responsibility of the
programmer to decide how to define the
function, based on run time and
compilation-time considerations?

The inline keyword is just a hint from the programmer to the compiler that the programmer would really like this function to be inlined (presumably the programmer has spotted substantial call overhead with a small function being called in "hot" loops as shown by the profiler -- or, the function is so tiny that it's about as small as the calling code;-) -- or, inlining the function allows "optimization across function boundaries" that a particular compiler can't spot or can't perform otherwise -- and so forth).

The compiler is free to ignore the hint just as it's free to ignore the older register hint for a variable's storage class (I believe nowadays most optimizing C++ compilers do ignore register but fewer ignore inline) -- IOW, the compiler is free to inline all or some of the calls to a function whether that function is declared inline or not.

(Of course it won't "inline calls" when they're done through an explicit pointer to the function that is stashed away at some points and used at others, or when the function's address is passed as a parameter to some other function - but that might affect the inlining of specific calls, not necessarily other calls to the same function that are done differently).

"Just in case" your compiler takes your inline hints very earnestly, it's often worth measuring code size and speed with and without inline around (unless your compiler offers an option/flag for the purpose, just a #define inline will definitely "disable" the effect of inline and thereby allow such measurement). But if you're going to deploy your code across multiple compilers, esp. for multiple architectures, be aware that the effects that are positive on one platform may end up being counterproductive on another, given the difference in compilers' optimizaiton strategies and in CPU architectures.

断桥再见 2024-09-19 23:30:30

函数需要什么条件才能在 C++ 中内联执行?有没有函数不能内联的情况?

内联函数只是用实际代码替换函数调用的一种函数。如果您按照该定义,您可以手动复制并粘贴代码,它将是“内联”的。然而,这可能并不总是一个好主意:这是速度与程序大小的问题。随着函数变得越来越大,内联它们的好处就会减少。

或者任何函数都可以是内联的,程序员有责任根据运行时和编译时的考虑来决定如何定义函数?

通常,当您使用inline关键字时,它只是一个请求,编译器会选择是否实际使函数调用内联。许多编译器还提供了一种强制函数内联的方法(例如,MSVC 有 __forceinline 关键字) - 在这种情况下,编译器不会试图变得聪明,所以这取决于你权衡使函数内联的好处。

what are the requirement for a function so it could be executed inline in c++? is there a case that a function can't be inline?

An inline function is just one where the function call is replaced with the actual code. If you go by that definition, you could copy and paste the code manually and it would be "inline". However, that may not always be a good idea: it's a matter of speed vs. program size. As functions grow larger, the benefit of having them inline reduces.

or any function can be inline and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

Normally, when you use the inline keyword, it is only a request and the compiler chooses whether or not to actually make the function call inline. Many compilers also provide a way to force a function to be inline (for eg. MSVC has the __forceinline keyword) - in this case, the compiler doesn't try to be smart, so it's up to you to weigh the benefits of making the function inline.

奶茶白久 2024-09-19 23:30:30

或者任何函数都可以是内联的,程序员有责任根据运行时和编译时的考虑来决定如何定义函数?

是编译器根据某些条件将该函数用作内联函数。

or any function can be inline and it is the responsibility of the programmer to decide how to define the function, based on run time and compilation-time considerations?

It's the compiler that basing on some conditions uses the function as inline function.

橙幽之幻 2024-09-19 23:30:30

内联函数取决于代码的长度和代码的复杂性......:)
内联函数只是对编译器的请求......

inline functions depends upon the length of the code and the complexity of the code...:)
The inline function is just a request to the compiler...

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