本文介绍了给定一个lambda捕获,什么规则确定生成的闭包成员的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何从捕获中精确预测将在lambda中创建哪种类型的成员 在C ++中,我认为捕获类型 T 由值创建 const T 类型的数据成员,并通过引用 T& 。但编译时: #include< iostream> struct A { A(){std :: cout<<A\\\;} A(const A&){std :: cout< <A& \\\;} void cf()const {} void f(){} }; int main(){ A a; A& ra = a; const A& cra = a; auto f00 = [ra,cra,& a]() - > void { // Fixed: // ra is A,cra is const A,a is A& // lambda是void operator()()const a.cf(); a.f(); // pass //ra.cf(); ra.f(); // ra.f编译错误。 //cra.cf(); cra.f(); // cra.f compilation err }; // f00(); // A&,A& auto f01 = [ra,cra,& a]()mutable-> void { // Fixed: // ra is A,cra is const A,a is A& // lambda是void operator()()mutalbe a.cf(); a.f(); // pass ra.cf(); ra.f(); // pass cra.cf(); cra.f(); // cra.cf pass,但是cra.f错误,为什么? }; // f01(); // A&,A& auto f02 = [& ra,& cra,& a]()mutable-> void { // Fixed: // ra is A& cra is const A& a is A& // lambda是void operator()()mutable a.cf(); a.f(); // pass ra.cf(); ra.f(); // pass //cra.cf(); cra.f(); // cra.cf pass,但是cra.f错误,为什么? }; f02(); // return 0; } 我遇到以下编译错误: test_lambda.cpp:26:25:error:传递'const A'作为'this'参数丢弃限定符[-fpermissive] cra.cf ; cra.f(); // pass,cra.f error ^ test_lambda.cpp:8:10:注意:在调用'void A :: f这是否意味着 cra 解决方案 p>捕获的实体的类型保持不变,除了对对象的引用被捕获为引用对象的副本。从 Lambda关闭类型的CPP参考:在所有的lambdas中,闭包成员 cra 的类型是 A 。它们本身不是 const 。但是,lambda的默认函数调用 operator()是。第17行关于 f00 的错误是由于尝试在调用 ra.f()时复制通过复制创建的闭包成员而引起的错误,但由于它有一个 operator()const ,它只能执行 const 这是为什么在所有三个函数中调用非 const A :: f code> cra 给出编译错误。您应该在lambda参数列表之后添加 mutable ,以允许对副本关闭成员执行非 - const 操作。 / p> How can I accurately predict from a capture which type of member will be created in the lambda?In C++, I thought that capturing an object of type T by value creates a data member of type const T, and by reference T&. But when compiling this:#include <iostream>struct A{ A(){std::cout<<"A\n";} A(const A&){std::cout<<"A&\n";} void cf()const{} void f(){}};int main(){ A a; A& ra=a; const A& cra=a; auto f00 = [ra, cra, &a]()-> void{ //Fixed: //ra is A, cra is const A, a is A& //lambda is void operator()()const a.cf(); a.f();//pass //ra.cf(); ra.f();//ra.f compilation err. //cra.cf(); cra.f();//cra.f compilation err }; //f00(); //A&,A& auto f01 = [ra, cra, &a]()mutable-> void{ //Fixed: //ra is A, cra is const A, a is A& //lambda is void operator()()mutalbe a.cf(); a.f();//pass ra.cf(); ra.f();//pass cra.cf(); cra.f();//cra.cf pass, but cra.f error, why? }; //f01(); //A&,A& auto f02 = [&ra, &cra, &a]()mutable-> void{ //Fixed: //ra is A&, cra is const A&, a is A& //lambda is void operator()()mutable a.cf(); a.f();//pass ra.cf(); ra.f();//pass //cra.cf(); cra.f();//cra.cf pass, but cra.f error, why? }; f02(); // return 0;}I encountered the following compilation error:test_lambda.cpp:26:25: error: passing 'const A' as 'this' argument discards qualifiers [-fpermissive] cra.cf(); cra.f();//pass, cra.f error ^test_lambda.cpp:8:10: note: in call to 'void A::f()' void f(){} ^Does this mean that cra has really been captured by reference, rather than by a copy of the referred object as I expected? 解决方案 The type of the captured entities remains the same, except that references to objects are captured as copies of the referenced objects. From CPP Reference on Lambda Closure Types:In all of your lambdas, the type of the closure member cra is A. They are not, themselves, const. However, the default function-call operator() of the lambda is. The error on line 17 about f00 is caused by the fact that you attempt to modify an closure member created by copy when calling ra.f(), but due to it having an operator() const, it can only perform const operations on its members.This is why in all three functions calling the non-const A::f on cra gives a compilation error. You should add mutable after the lambda argument list to allow performing non-const operations on by-copy closure members. 这篇关于给定一个lambda捕获,什么规则确定生成的闭包成员的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 08:00