【发布时间】:2012-09-14 22:38:59
【问题描述】:
C++11 模式下的 GCC 4.7 允许我定义一个采用 lambda 的函数,有两种不同的方式:
// 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 传递给最后一个?
【问题讨论】:
标签: c++ templates lambda c++11 g++