Confusing return by value

Source: Internet
Author: User

 

The problem starts here.
Class X;
Const x operator + (const X & X1, const X & x2 );
X Foo ()
{
Return X (A + B );
}
And
X Foo ()
{
X xx (A + B );
Return xx;
}
What are the differences between these two functions?
This problem involves the internal processing of C ++. The following is a more common mode.
X Foo ()
{
X xx;
// Process
...
Return xx;
}
The traditional method for C ++ to handle return by value is to modify the function prototype.
Void Foo (X & R)
{
X xx;
XX. X (); // ctor
// Process
...
R. xx (XX) // copy ctor
}
In this way, the value of XX is returned. Here there is a ctor and dtor of the temporary object xx.
Thus.
X obj = Foo ();
Converted
X obj;
Foo (OBJ );
However, for programmers, some optimizations can be made. The method is to use the ctor return function,
X Foo ()
{
Return X (...);
}

This is the case after the compiler processes it.
Void Foo (X & R)
{
Return R. X (...);
}
See it? There are only a few objects (XX above) ctor and dtor, which naturally improves the efficiency.
Further, if this function is inline
X obj = Foo ();
Processed
X obj;
OBJ. X (...);
Bingo !!
It is really awesome, and the efficiency of C ++ is fully reflected.
This kind of optimization is called ARV (anonymous return value) optimization ,:)
However, what should we do for the former nrv (named return value?
The answer is: there is no way. The only thing you can do is look forward to the compiler for some help.
If our compiler has enough power, then
X Foo ()
{
X xx;
// Process
...
Return xx;
}
It may be processed as follows:
Void Foo (X & R)
{
// Ctor
R. X ();
// Process
...
}
The named object (XX) ctor and dtor are not displayed here, but as a programmer, I advise you not to count too much on it.
First, it calls the default ctor, which does not exist in ARV.
Secondly, you cannot know whether the compiler is doing this. After all, for the manufacturer, it has the right not to let you know the details.
Again, even if the compiler has such capabilities, it is only effective for some simple functions. For actual programming, functions are often very complex,
For example, the compiler can only sigh when there are functions returned by multiple branches.
In addition, the object declaration method at the beginning of the function is not an authentic C ++ style. c ++ always advocates that "only when necessary ", real C ++ programmers should keep this in mind.
Finally, from the perspective of C ++ compiler history, in the optimization process, anonymous objects are easier to eliminate than named objects, and you must make full use of them.
Answer:
For such a simple function, if your compiler supports nrv optimization, the result is the same, otherwise...
ARV optimization is always supported.
Does ARV have any disadvantages?
Maybe, after all, you have to write a bunch of cuts for special purposes. :)
Note:
The example in this article is chosen from <inside C ++ object model>. Thanks to Lippman for writing such a good book to C ++ programmers, thanks to jjhou for translating it to Chinese programmers.
This article also references MEC (more effective C ++)

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.