【发布时间】:2015-10-03 07:14:34
【问题描述】:
所以我有一个完美的转发器,我想在 lambda 中适当地捕获它,以便复制 R 值,并通过引用捕获 L 值。然而,简单地使用 std::forward 并不能完成这项工作,正如这段代码所证明的那样:
#include<iostream>
class testClass
{
public:
testClass() = default;
testClass( const testClass & other ) { std::cout << "COPY C" << std::endl; }
testClass & operator=(const testClass & other ) { std::cout << "COPY A" << std::endl; }
};
template< class T>
void testFunc(T && t)
{ [test = std::forward<T>(t)](){}(); }
int main()
{
testClass x;
std::cout << "PLEASE NO COPY" << std::endl;
testFunc(x);
std::cout << "DONE" << std::endl;
std::cout << "COPY HERE" << std::endl;
testFunc(testClass());
std::cout << "DONE" << std::endl;
}
用
编译g++ -std=c++14 main.cpp
产生输出
PLEASE NO COPY
COPY C
DONE
COPY HERE
COPY C
DONE
在一个完美的世界里,我只想让“COPY C”出现在右值的情况下,而不是左值的情况下。
我的解决方法是使用为 L- 和 R- 值重载的辅助函数,但我想知道是否有更好的方法。
干杯!
【问题讨论】:
-
@NirFriedman 这个问题解释了移动捕获和整个通用机制,但我认为它没有为完美转发捕获提供任何答案。直接的方法(OP 尝试过的)显然行不通。
-
@Angew 不错,我的错。向 OP 道歉。
-
无需道歉 :-)
标签: c++ lambda perfect-forwarding