模板化函数应该通过值还是通过右值引用来获取 lambda 参数?

Should templated functions take lambda arguments by value or by rvalue reference?(模板化函数应该通过值还是通过右值引用来获取 lambda 参数?)
本文介绍了模板化函数应该通过值还是通过右值引用来获取 lambda 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++11 模式下的 GCC 4.7 让我定义了一个采用 lambda 的函数,有两种不同的方式:

GCC 4.7 in C++11 mode is letting me define a function taking a lambda two different ways:

// by value
template<class FunctorT>
void foo(FunctorT f) { /* stuff */ }

还有:

// by r-value reference
template<class FunctorT>
void foo(FunctorT&& f) { /* stuff */ }

但不是:

// by reference
template<class FunctorT>
void foo(FunctorT& f) { /* stuff */ }

我知道我可以取消模板函数并只使用 std::functions 代替,但是 foo 很小并且是内联的,我想给编译器最好的机会来内联调用 f 它在里面.在前两个中,如果我明确知道我正在传递 lambda,那么这对于性能来说更可取,为什么不允许将 lambda 传递给最后一个?

I know that I can un-template the functions and just take std::functions instead, but foo is small and inline and I'd like to give the compiler the best opportunity to inline the calls to f it makes inside. Out of the first two, which is preferable for performance if I specifically know I'm passing lambdas, and why isn't it allowed to pass lambdas to the last one?

推荐答案

FunctorT&& 是一个通用引用,它可以匹配任何东西,而不仅仅是右值.这是在 C++11 模板中传递内容的首选方式,除非您绝对需要副本,因为它允许您使用完美转发.通过 std::forward<FunctorT>(f) 访问该值,这将使 f 再次成为右值(如果它是以前的),否则将其保留为左值.阅读更多这里关于转发问题和std::forward并阅读这里了解 std::forward 如何真正工作的分步指南.这也是一本有趣的读物.

FunctorT&& is a universal reference and can match anything, not only rvalues. It's the preferred way to pass things in C++11 templates, unless you absolutely need copies, since it allows you to employ perfect forwarding. Access the value through std::forward<FunctorT>(f), which will make f an rvalue again if it was before, or else will leave it as an lvalue. Read more here about the forwarding problem and std::forward and read here for a step-by-step guide on how std::forward really works. This is also an interesting read.

FunctorT& 只是一个简单的左值引用,您不能将临时变量(lambda 表达式的结果)绑定到它.

FunctorT& is just a simple lvalue reference, and you can't bind temporaries (the result of a lambda expression) to that.

这篇关于模板化函数应该通过值还是通过右值引用来获取 lambda 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)