是在c ++ inline中的lambda函数 [英] are lambda functions in c++ inline

查看:614
本文介绍了是在c ++ inline中的lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是c ++ inline中的lambda函数?
编译器可以优化内联函数的调用以提高效率。
的问题是:可以编译器优化lambda函数的调用,就像它对简单的内联函数。
例如。

are lambda functions in c++ inline? Compiler can optimise calls of inline functions to increase efficiency. the question is: can compiler optimise calls of lambda function like it does for simple inline ones. e.g.

std::vector<double> vd;
std::for_each(vd.begin(), vd.end(), [](const double d) {return d*d;});

是否因缺乏优化而导致效率损失?

are there loss of efficiency caused by lack of optimisation?

另一个问题:我可以检查编译器是否使用优化调用的内联函数,它被发送到算法(我的意思是:如果函数 - 不是函数对象 - 发送到算法,最后一个获取指针)

and another question: where I can check if the compiler I use optimise calls of inline functions, which are sent to algorithm (I mean: if function - not function object - is sent to algorithm, the last one gets pointer to the function, and some compilers do optimisation for pointer to inline function and other don't.)?

推荐答案

在简单的情况下,就像你的例子一样,你应该期望使用lambdas比函数指针更好的性能,参见

In simple cases, like your example, you should expect better performance with lambdas than with function pointers, see

为什么lambdas可以比纯函数更好地由编译器优化?

正如其他人已经指出的,不能保证你的电话会被内联,但你有更好的机会与lambdas。检查呼叫是否已内联的一种方法是检查生成的代码。如果使用gcc,则将-S标志传递给编译器。当然,它假设你可以理解汇编代码。

As others have already pointed out, there is no guarantee that your call will be inlined but you have better chances with lambdas. One way of checking whether the call has been inlined is to check the generated code. If you are using gcc, pass the -S flag to the compiler. Of course, it assumes that you can understand the assembly code.

这篇关于是在c ++ inline中的lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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