In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 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++ should use return values rather than output parameters when outputting results. The explanation in this article is simple and clear, and easy to learn and understand. Please follow the editor's ideas slowly and deeply. Let's study and learn why C++ should use return values rather than output parameters when outputting results.
You should use the return value rather than the output parameter Reason when outputting the result (reason)
The return value itself can indicate the purpose, while the reference type can be an input / output parameter or just an output parameter, which can be easily misused.
This view can cover large objects such as standard containers, which use implicit move operations for performance and avoid explicit memory management.
If you have multiple values to return, use tuple or similar multi-member types.
Example (example) / / OK: return pointers to elements with the value xvector find_all (const vector&, int x)
/ / Bad: place pointers to elements with value x in-outvoid find_all (const vector&, vector& out, int x)
Note (Note)
Structures that contain multiple elements (which can be moved at low cost individually) can be costly to move together.
Returning a constant value is not recommended. This outdated advice is now obsolete; it does not benefit, and its interface contains mobile semantics.
Const vector fct (); / / bad: that "const" is more trouble than it is worth
Vector g (const vector& vx) {/ /... Fct () = vx; / / prevented by the "const" / /... Return fct (); / / expensive copy: move semantics suppressed by the "const"}
The view that it is recommended to add const decorations to the return value is that this prevents (rarely occurring) unexpected access to temporary variables. The opposite view is that doing so will (very much) prevent the use of mobile semantics.
Exceptions (exception)
For non-value types, such as types in an inheritance hierarchy, return the object by unique_ptr or shared_ptr.
For non-value type functions, such as types in an inheritance relationship, the object is returned through unique_ptr or shared_ptr.
Translator's note: both methods can avoid unnecessary copying actions.
If a type is expensive to move (e.g., array), consider allocating it on the free store and return a handle (e.g.unique_ptr), or passing it in a reference to non-const target object to fill (to be used as an out-parameter).
If the moving cost of a type, such as array, is high, consider requesting memory for it from free storage and returning it using a handle (such as unique_prt), or passing it through a reference to a non-constant object used to populate.
Translator's note: POD, the abbreviation of Plain old data structure, is a kind of data structures defined in the standard of C++ language. It is easy to understand structures that contain only simple data types.
To reuse an object that carries capacity (e.g.std::string, std::vector) across multiple calls to the function in an inner loop: treat it as an in/out parameter and pass by reference.
In order for a function call in an inner loop to reuse an object with capacity (such as std::string,std::vector): treat it as an input / output parameter and pass it through a reference.
Example (sample)
Struct Package {/ / exceptional case: expensive-to-move object char header [16]; char load [2024-16];}
Package fill (); / / Bad: large return valuevoid fill (Package&); / / OK
Int val (); / / OKvoid val (int&); / / Bad: Is val reading its argument
Translator's note: the sample code shows that POD uses references to pass output values, while small data users should use return values directly.
Enforcement (implementation recommendations)
Flag reference to non-const parameters that are not read before being written to and are a type that could be cheaply returned; they should be "out" return values.
Warn those parameters that are not read before writing (for no input purpose) and can be returned at a low cost, they should be output as return values.
Flag returning a const value. To fix: Remove const to return a non-const value instead.
Warning returns a constant value. Modification method: remove the constant modifier and return a non-constant quantity.
Thank you for your reading, the above is the "why C++ output results should use the return value rather than the output parameters" content, after the study of this article, I believe that why C++ output results should be more use of return values rather than output parameters of this question have a deeper understanding, the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.