duang!!! Why functions can return unique_ptr

Source: Internet
Author: User

C + + Abuse me times, I am C + + like First love
Speaking from the smart pointerFor a master, the pointer is the artifact of heaven; for beginners, it is a source of disaster. High-level languages such as java,c# automatically manage memory, so you don't have to worry about memory-release issues.Bjarne StroustrupC + + adds that the garbage collection mechanism will not be suitable for the development of the underlying system, so C + + advocates using RAII to manage resources. Auto_ptr is a smart pointer born of this idea, intended to write an efficient approach to the native pointer, but with the resource ownership security of the smart pointer. When an assignment occurs, the ownership is transferred when the copy is constructed. This is very chicken, can not be placed in the STL container, because its copy construction and traditional copy construction is not the same.
Unique_ptr replaces Auto_ptrC++11 's rvalue reference and move semantics solve this problem, so Unique_ptr replaces Auto_ptr. Why Unique_ptr can be put into a container? Such as:vector<unique_ptr<song>> v;V.push_back (unique_ptr<song> ("Thenew Song (" B ' Z "," Juice " ));
The answer is UNIQUE_PTR can move, not copy. It does not copy the construction, copy the assignment, but has the move construct, the move assigns the value. Although it can be placed inside a container, not all functions can be used, of course, when necessary, you can use Std::move to convert the Lvalue to the right value.
Now, there's a problem with unique_ptr without the copy function, so how does the function return unique_ptr? For example, C++14 has a make_unique template function that returns UNIQUE_PTR. Let's take a look at his implementation:template<class T, class ... Types>unique_ptr<t> Make_unique (types&& Args){return (unique_ptr<t> (New T (Forward<types> (Args))); }This function calls the copy construct, and at first I was confused, after a search, it was basically possible to confirm that the copy construction was not called.
Rvo and NrvoWhen a function returns an object, a temporary variable is theoretically generated, which inevitably results in the construction of the new object and the destruction of the old object, which has an effect on efficiency. C + + compilation for this situation allows optimization, even if the constructor has side effects, this is called Return Value optimization (RVO), the return of the name of the object is called the Named Return Value optimization (NRVO), that is, RVO, it is to return the temporary object to be generated, The construction of the returned object is now constructed directly in the space that accepts the returned object. Assuming that the return value is not optimized, will it be problematic to return unique_ptr? It's not. Because the standard allows the compiler to do this: 1. If the move construct is supported, then the move construct is called. 2. If move is not supported, then call the copy construct. 3. If copy is not supported, then error.
Obviously, UNIQUE_PTR is supported by the move construct, and the Unique_ptr object can be returned by the function.

duang!!! Why functions can return unique_ptr

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.