是否可以内联 lambda 表达式?
我想内联 lambda 表达式,因为出于性能原因它非常短。是否可以?
I want to inline a lambda expression since it is very short for performance reason. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
inline
关键字实际上不会导致函数内联。任何最近的编译器在内联方面都会比您做出更好的决定。在 lambda 较短的情况下,该函数可能会被内联。
如果您尝试将
inline
关键字与 lambda 一起使用,答案是否定的,您不能使用它。The
inline
keyword does not actually cause functions to be inlined. Any recent compiler is going to make better decisions with regards to inlining than you will.In the case of a short lambda, the function will probably be inlined.
If you're trying to use the
inline
keyword with a lambda, the answer is no, you can't use that.如果可以的话,编译器将内联它。例如,在 g++ 4.5 中使用 -O2,
生成
2*x
和printf
lambda 完全内联的程序集。The compiler will inline it if it can. For example, in g++ 4.5 with -O2,
generates the assembly that the
2*x
andprintf
lambdas are completely inlined.lambda 表达式可能是内联的。从本质上讲,lambda 表达式与任何其他函数对象没有什么不同。
特定的 lambda 表达式是否内联完全取决于编译器以及编译器是否决定其值得内联。
It is possible that a lambda expression might be inlined. Under the hood, a lambda expression is no different than any other function object.
Whether a particular lambda expression is inlined is entirely dependent upon the compiler and whether it decides it is worth inlining.
如果你有一个常规的结构函子,编译器几乎肯定会内联它。如果您有 C++0x 风格的 lambda,编译器几乎肯定会内联它。如果您使用的是 boost::lambda,那么它可能会这样做,具体取决于 lambda 在幕后的工作方式。简短版本:您不能保证它是内联或非内联,但您应该相信您的编译器,如果有疑问,请使其轻松简单地内联。
If you have a regular struct functor, the compiler will almost certainly inline it. If you have a C++0x style lambda, the compiler will almost certainly inline it. If you're using a boost::lambda, then it might do, depending on how the lambda works underneath the scenes. Short version: You can't guarantee it's inlining or non-inlining, but you should trust your compiler and if in doubt, make it easy and simple to inline.
我还没有以任何方式查看其中很多的输出,但到目前为止,我查看过的所有输出都已内联。
I haven't looked at the output from a lot of them by any means, but so far all of them I've looked at the output from have been inlined.
C++1x' lambdas 将在底层生成普通函数对象。这些可以由编译器内联。
您所做的任何测量是否表明编译器没有将它们内联到导致显着性能损失的位置?
C++1x' lambdas will, under the hood, generate normal function objects. Those can be inlined by the compiler.
Did any measurements you made suggest the compiler did not inline them in a place where this leads to a notable performance loss?