相关疑难解决方法(0)

在 lambda 中完美捕获完美转发器(通用参考)

所以我有一个完美的转发器,我想在 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 …
Run Code Online (Sandbox Code Playgroud)

c++ lambda perfect-forwarding

5
推荐指数
1
解决办法
1171
查看次数

标签 统计

c++ ×1

lambda ×1

perfect-forwarding ×1