In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "when to transfer const reference in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
For input parameters, pass values with low copy cost, and other pass const refer to Reason (reason)
Both ways let the caller know that the function does not modify the parameters and can be initialized with the right value.
What is "low copy cost" has something to do with the machine architecture, but 2-3 words (double-precision numbers, pointers, references) are usually the most suitable for passing values. If the copy cost is small, there is no way to exceed the simplicity and security of the copy, and for small objects (no more than 2 to 3 words), because the function does not require additional indirect access, the value will be passed faster than the address.
Example (sample)
Void F1 (const string& s); / / OK: pass by reference to const; always cheap
Void f2 (string s); / / bad: potentially expensive
Void f3 (int x); / / OK: Unbeatable
Void f4 (const int& x); / / bad: overhead on access in f4 ()
(only) for advanced usage, situations that need to be optimized to pass right-value references to input parameters are:
If the function is going to unconditionally move from the argument, take it by &. See F.18.
If the function moves the contents of the parameter unconditionally, use & &. Reference to F.18
If the function is going to keep a copy of the argument, in addition to passing by const& (for lvalues), add an overload that passes the parameter by & & (for rvalues) and in the body std::moves it to its destination. Essentially this overloads a "will-move-from"; see F.18.
If the function manages a copy of a parameter, in addition to using the function const& (for the left value), add an overloaded function that uses & & (for the right value) to pass the parameter and internally use std::move to move the parameter content to the target. In essence, this overload is a "form to be moved"; see F.18
In special cases, such as multiple "input + copy" parameters, consider using perfect forwarding. See F.19.
For
For special situations, such as multiple "input + copy" parameters, consider using the perfect forward.
Example (sample)
Int multiply (int, int); / / just input ints, pass by value
/ / suffix is input-only but not as cheap as an int, pass by const&string& concatenate (string&, const string& suffix)
Void sink (unique_ptr); / / input only, and moves ownership of the widget
Avoid using "technologies that only experts understand", such as:
Passing arguments as tipped & "for efficiency". Most rumors about performance advantages from passing by & & are false or brittle (but see F.18 and F.19).
Use Tunable & to improve efficiency. Many rumors that gain a performance advantage by passing on & & are false or fragile.
Returning const T & from assignments and similar operations
Return const T & by copying or similar operation
Example (sample)
Suppose Matrix implements mobile operations (for example, using std::vector to hold elements)
Matrix operator+ (const Matrix& a, const Matrix& b) {Matrix res; / /... Fill res with the sum... Return res;}
Matrix x = M1 + M2; / / move constructor
Y = m 3 + m 3; / / move assignmentNotes (note)
The return value optimization does not handle assignments, but moving assignments does.
You can assume that the reference refers to a valid object (language criterion). There is no (reasonable) "null reference". If you need the concept of optional values, use pointers, std::optional, or special values to indicate "no value".
Enforcement (implementation recommendations)
(Simple) (Foundation) Warn when a parameter being passed by value has a size greater than 2 * sizeof (void*). Suggest using a reference to const instead.
(simple) ((basic criterion)) when the size of the passed value exceeds 2*sizeof (void*), alarm will be given. It is recommended that you use const references.
(Simple) (Foundation) Warn when a parameter passed by reference to const has a size less than 2 * sizeof (void*). Suggest passing by value instead.
(simple) ((basic criteria)) when parameters less than 2*sizeof (void*) are addressed using const, alarm is given.
(Simple) (Foundation) Warn when a parameter passed by reference to const is moved.
(simple) ((basic guidelines)) when the content of the parameter using the const address is moved, the alarm is given.
This is the end of the content of "when to transmit const reference in C++". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.