简体   繁体   English

C ++ 17 lambda capture * this

[英]C++17 lambda capture *this

C++17 will add copy capture of this object by value, with a capture specification of [*this] . C ++ 17将按值添加此对象的副本捕获捕获规范为[*this]

How is this useful? 这有用吗? How is it different than capturing this ? 它是如何捕捉比不同this Can't this already be achieved in C++14 with [tmp = *this] ? [tmp = *this]在C ++ 14中已经不能实现[tmp = *this]吗?


Bonus for explaining why P0018R3 uses [=, tmp = *this] instead of [tmp = *this] in their example. 用于解释为什么P0018R3在其示例中使用[=, tmp = *this]而不是[tmp = *this]奖励。 If they had used [tmp = *this] , all the listed downsides of the C++14 solution would be eliminated. 如果他们使用了[tmp = *this] ,那么C ++ 14解决方案的所有列出的缺点都将被消除。

How is it useful? 它有用吗? It's useful when you need a copy of *this - for example, when *this itself is no longer valid by the time the lambda is evaluated. 当你需要*this的副本时它很有用 - 例如,当lambda被评估时, *this本身不再有效。

How is it different from capturing this ? 它是如何从不同捕捉this It makes a copy of the object, so that when the lambda is evaluated, its this pointer refers to the copy, rather than to the original object. 它创建了对象的副本 ,因此在评估lambda时,它的this指针引用副本,而不是原始对象。

Can it be achieved in C++14 with [tmp = *this] ? 可以用[tmp = *this]在C ++ 14中实现吗? It can, but [*this] is more convenient, as code can be moved without prefixing member access with tmp. 它可以,但[*this]更方便,因为可以移动代码而无需使用tmp.为成员访问添加前缀tmp. . Otherwise, especially with [=, tmp = *this] , it's possible to accidentally refer to members of the original object when you meant to refer to the copy (particularly if you're in the habit of cut+paste programming). 否则,特别是[=, tmp = *this] ,当您打算引用副本时(特别是如果您习惯于剪切+粘贴编程),可能会意外地引用原始对象的成员。 [=,*this] is a safer alternative in this case, as the original object is not accessible from inside the lambda's body (at least, not via the this pointer). [=,*this]在这种情况下是一个更安全的替代方案,因为原始对象不能从lambda体内部访问(至少不能通过this指针)。

Imagine that *this is a handle class, which maintains a shared_ptr to some shared state. 想象一下*this是一个句柄类,它将shared_ptr维护到某个共享状态。

The shared impl is (for example) a protocol handler state machine. 共享impl是(例如)协议处理程序状态机。

The handle class is passed through a series of asynchronous handlers, so itself must be copyable. handle类通过一系列异步处理程序传递,因此它本身必须是可复制的。 Each handler mutates the shared state. 每个处理程序都会改变共享状态。

A strong use case for this might be a protocol handler for use with a custom asio service (for example, an http_protocol_socket ). 一个强大的用例可能是与自定义asio服务一起使用的协议处理程序(例如, http_protocol_socket )。

[=, tmp = *this] will promiscuously capture any variables by value, including, rather dangerously, the this pointer itself, as well as specifically capturing *this into tmp . [=, tmp = *this]将以值的形式捕获任何变量,包括相当危险的, this指针本身,以及特别是将*this捕获到tmp

In this use case it would be dangerous to inadvertently refer to this in the async handler, because it's likely to be a dangling pointer. 在这种情况下,使用它会是危险的不经意指this在异步处理程序,因为它很可能是一个悬摆指针。 This is a bug waiting to happen. 这是一个等待发生的错误。

[tmp=*this] will only capture *this . [tmp=*this]只捕获*this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM