当前位置:网站首页>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 :
- from
lambdaThe name of the member variable in the generated closure class - 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 :
- Move the object to be captured to
std::bindIn the generated function object - Give to the
lambdaType 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));
边栏推荐
- Analysis of the principle of V-model and its application in user defined components
- Sampo Lock
- Must know must know -c language keywords
- 【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
- ES6 learning
- QT signal is automatically associated with the slot
- Logical point
- Elliptic curve encryption
- Nodejs parsing get request URL string
- 2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
猜你喜欢
随机推荐
CMB written test graphical reasoning
2022 chlorination process operation certificate examination question bank and simulation examination
PowerShell:因为在此系统上禁止运行脚本,解决方法
Use go to add massive data to MySQL
PowerShell plus domain add computer module
LeetCode第297场周赛(20220612)
Kaggle 时间序列教程
Brick story
Common skills in embedded programming
Section 5 - Operator details
利用Javeswingjdbc基於mvc設計系統
QT direction key to move focus
QT realizes message sending and file transmission between client and server
ES6 learning
How to lay copper in AD (aluminum designer)
Design system based on MVC using javeswingjdbc
Stepping on a horse (one stroke)
Elliptic curve encryption
Basic syntax example for go
QT client development -- driver loading problem of connecting to MySQL database









