In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains why C++ does not return to the right value reference, interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "why C++ does not return to the right value reference"!
Reason (reason)
Returning a reference to the right value is a reference to a temporary object that has been destroyed. & & is attractive to temporary objects.
Example (sample)
The right value reference returned exits the valid range when the last return of all expressions.
Auto&& x = max (0,1); / / OK, so farfoo (x); / / Undefined behavior
This usage is a common cause of bug. Is often mistakenly reported as a compiler error. The implementer of the function should avoid setting such a trap for the user.
The lifetime safety profile will (when completely implemented) catch such problems.
The lifecycle security rules group captures such problems (when fully implemented).
Example (sample)
It works well when the referenced object is passed down (inward) to the caller and returns a right value reference. In this case, it is guaranteed that the lifetime of the temporary variable is longer than the function call (see F.18 and F.19). However, there is a problem when passing this reference up (out) to a larger caller space. For pass-through functions that pass in input parameters (through the original reference or perfect fowrard handling) and need to return a value, use a simple auto type to return type inference (not auto&&).
Assume that F returns by value:
Suppose F is returned by value:
Templateauto&& wrapper (F f) {log_call (typeid (f)); / / or whatever instrumentation return f (); / / BAD: returns a reference to a temporary}
Better (better):
Templateauto wrapper (F f) {log_call (typeid (f)); / / or whatever instrumentation return f (); / / OK} Exception (exception)
Std::move and std::forward also return & &, but by convention they are only used in cases where references to a temporary variable are passed within the same expression before it is destroyed.
Enforcement (implementation recommendations)
Flag any use of & & as a return type, except in std::move and std::forward.
Tags use & & for the use of return value types, except for std::move and std::forward.
At this point, I believe you have a deeper understanding of "why C++ does not return the right value reference". 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.
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.