当前位置:网站首页>Clause 32: moving objects into closures using initialization capture objects

Clause 32: moving objects into closures using initialization capture objects

2022-06-13 04:56:00 CCSUZB

If you want to move an object only (std::unique_ptr or std::future) Put closure ,C++11 There is no way to do this . and C++14 Provides support for moving objects into closures .

Use initialization capture , Then you will be given the opportunity to specify :

  1. from lambda The name of the member variable in the generated closure class
  2. An expression , Used to initialize the member variable

Consider the following code :

class widget
{
    
public:
    bool isValidated() const;
    bool isProcessed() const;
    bool isArchived()  const;
private:
};

auto pw = std::make_unique<widget>();	//  To configure widget
...										//  To configure  *pw
auto func = [pw = std::move(pw)] {
    		//  use sd::move(pw),C+14 Initialize the data members of the closure class 
	return true;
};

pw = std::move(pw) Capture for initialization ,= On the left is the scope of the closure class , The scope on the right is the same as lambda The scope where the expression is defined is the same .= The name on the left pw Refers to Member variables of closure class , And the name on the right pw Reference is stay lambda The object declared on the above line

If you want to use lambda type , Press move to capture in C+11 The following methods can be used to simulate , It only needs :

  1. Move the object to be captured to std::bind In the generated function object
  2. Give to the lambda Type one refers to desire “ Capture ” In the object reference of

Suppose you want to create a local std::vector object , Put an appropriate set of values into it , Then move it into the closure . stay C++14 It's a small effort :

std::vector<int> data;
auto func = [data = std::move(data)]{
    };

Use C++11 The equivalent code written is as follows :

std::vector<double> data;
auto func = std::bind([](const std::vector<double>& data){
    return false;},
                      std::move(data));

std::bind The first argument is a callable object , All the next real parameters represent the values passed to the object .

By default ,lambsa In the generated closure class operator() Member functions have const An ornament . result , All member variables in the closure are in lambda The body of the function of the formula will have const An ornament . however , It is obtained by moving the structure in the binding object data The copy does not carry const An ornament . therefore , To prevent this data A copy of is in lambda The formula is accidentally modified ,lambda The formal parameter of is declared as a constant reference . But if lambda with mutable An ornament , In the closure operator Functions will not be declared with const An ornament , The corresponding approach , Is in the lambda Remove from the declaration const:

auto func = std::bind([](std::vector<double>& data) mutable
					  {
    return false;},
                      std::move(data));
原网站

版权声明
本文为[CCSUZB]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280518034262.html