In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use the reference form to capture local variables in lambda expressions. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
F.52 use references in lambda expressions to capture local variables, including cases where variables are passed to the algorithm.
Translator's note: if you are not sure what a capture is
Https://mp.weixin.qq.com/s/VuW_5OcX7uUUrMbsN2wxPQ
Reason (reason)
For the sake of efficiency and correctness, you almost always need to capture variables by reference when using lambda expressions locally. This includes writing or invoking local concurrency algorithms because they need to be converged before they are returned.
Discussion (discussion)
Considerations about efficiency: for most types, passing references is cheaper than passing values.
Considerations about correctness: many calls want (while the called function is executed) to have a collateral effect on the original object on the calling side (see the example below). Passing values can't do that.
Note (Note)
Unfortunately, for local calls, there is no simple way to capture constant type variables by reference to achieve efficiency and avoid collateral effects.
Translator's note: constant type applications should refer to reference parameters declared as constants in function declarations, which can provide both efficiency and write protection.
Example (sample)
Here, a large object (a network message) is passed to an iterative algorithm, and it is not efficient or correct to copy the message (it may not be copied):
Std::for_each (begin (sockets), end (sockets), [& message] (auto& socket) {socket.send (message);})
Translator's note: the cost of copying each cycle is also high.
Example (sample)
This is a simple three-phase concurrency pipeline. Each state object encapsulates a worker thread and a queue, and there is a process function that handles queuing, and its destructor automatically enters a blocking state to wait for the queue to empty before closing the thread.
Void send_packets (buffers& bufs) {stage encryptor ([] (buffer& b) {encrypt (b);}); stage compressor ([&] (buffer& b) {compress (b); encryptor.process (b);}); stage decorator ([&] (buffer& b) {decorate (b); compressor.process (b);}); for (auto& b: bufs) {decorator.process (b);}} / / automatically blocks waiting for pipeline to finish
Translator's note: implicit capture is used in the code. For more information, please refer to: https://mp.weixin.qq.com/s/RNpLqwekN3BJUbIN-DwVxQ
Enforcement (implementation recommendations)
Markup lambda expressions capture variables by reference, but are not used locally within the scope of the function, or are passed to another function by reference.
Note: this is an approximate rule, but it does mark: passing through pointers that are likely to be saved by the caller, writing somewhere on the heap accessed by parameters, returning lambda expressions, and so on. The lifecycle guidelines group also provides general guidelines for marking escape pointers and references, as well as cases derived from lambda expressions.
The above is how to use reference form to capture local variables in lambda expressions. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.