Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not implicitly capture this pointers in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Why do not implicitly capture this pointers in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "Why don't you implicitly capture this pointers in C++?"

F.54: if you need to capture this pointers, capture all variables explicitly (do not use implicit trapping).

Translator's note: implicit capture means that the variable name is ignored in the capture list, but only by using the same name as the variable outside the lamda expression. For example, in the negative sample code:

Auto lambda = [=] {use (I, x);}

Reason (reason)

This practice is difficult to understand. The capture list [=] in the member function looks like a value capture, but because it actually captures invisible this pointers as values, it actually captures data members by reference. If that's what you want to do, explicitly write this to the capture list.

Example (sample)

Class My_class {int x = 0; / /. Void f () {int I = 0; / /... Auto lambda = [=] {use (I, x);}; / / BAD: "looks like" copy/value capture / / [&] has identical semantics and copies the this pointer under the current rules / / [=, this] and [&, this] are not much better, and confusing x = 42; lambda (); / / calls use (0,42); x = 43; lambda (); / / calls use (0,43) / /... Auto lambda2 = [I, this] {use (I, x);}; / / ok, most explicit and least confusing / /...}}; Note (attention)

This is an issue that is less active in the standardization process and may be addressed by future versions of the standard by adding a new capture method or changing the meaning of [=]. At present, as long as it is clear.

Enforcement (implementation recommendations)

Prompt if the snap list of any lambda expression is defined to capture implicitly and at the same time capture the this pointer (either explicitly or by default).

At this point, I believe that you have a deeper understanding of "Why do not implicitly capture this pointers in C++", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report