Should templated functions take lambda arguments by value or by rvalue reference?(模板化函数应该通过值还是通过右值引用来获取 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 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模板化函数应该通过值还是通过右值引用来获取


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30