Exploring the C ++ Object Model in depth (9)

Source: Internet
Author: User
Tags visual studio 2010

========================================================== ==================================

If you like it, follow these steps:Jellythink | idea jelly

More original highlightsWww.jellythink.com

You can also follow Sina Weibo:Http://weibo.com/u/1887014677

========================================================== ==================================

Named return value (actually optimized return value)
When a function returns an instance of an object, a temporary object is created and its value is returned by copying the constructor. However, in the C ++ standard, it is incumbent on every C ++ compiler to allow the copying of constructor objects (provided that all paths return the same object) to be omitted.
There are the followingCode:

 # Include  Using   Namespace STD;  Class  Testa {  Public  : TESTA () {cout < "  Constructor  " < Endl ;} ~ Testa () {cout < "  Destructor  " < Endl;} TESTA (  Const Testa &Test) {cout < "  Copy constructor  " < Endl ;}}; Testa gettest () {Testa test;  Return  Test ;}  Int  Main () {Testa; Testa = Gettest ();  Return   0  ;} 

The function gettest obtains the object of a class. In the compiler, what is the actual processing of the function gettest? In my previous blog, I wrote about the time to call and not call copy constructor. The current gettest function is now a situation where copy constructor needs to be called. The output is as follows:

 
Constructor copy constructor destructor press any key to continue...

When running the above Code, the copy constructor is output. Yes, copy constructor is called. This is exactly what I mentioned in my previous blog post. The gettest function is extended by the compiler as follows:

VoidGettest (testa &Result) {Testa test; test. Testa: TESTA ();//Constructor//Do something with test object//...Result. Testa: TESTA (test );Return;}

Just as the preceding gettest function is expanded by the compiler, all paths return the same named value. Therefore, the compiler can perform its own optimization. Replace named return valued directly with result. Yes, the compiler needs optimization. By default, Visual Studio 2010 disables nrv. You can choose to disable nrv in Project Properties> Configuration Properties> C/C ++> optimization, all are not optimized. You can turn on this switch (/O2) and then compile the aboveProgram, Copy constructor is not called. This is the compiler optimization. The program output is as follows:

Constructorconstructordestructordestructor press any key to continue...

As you can see, the actual number of constructor and destructor indicates that the number of results generated in the gettest function is less, and no result generation and analysis structure is available. Now there is a copy constructor optimized and not called. What if there is no copy consturctor? I will comment out the copy consturctor above. The running result is as follows:

 
Constructorconstructordestructordestructordestructor press any key to continue...

The result of mingw compilation is as follows (even with copy consturctor ):

 
Constructorconstructordestructordestructor press any key to continue...

You will be surprised. How can it be different?
In the book "Deep Exploration of C ++ object model", the appearance of copy constructor activates nrv optimization in the C ++ compiler. nrv optimization is not completed through other independent optimization tools. It can be seen that there is a way out between the results obtained and those described in the book. In fact, even if the copy constructor and destructor are not defined but generated by the compiler (as needed), nrv will still affect the program efficiency. In Visual Studio 2010, a few constructor calls the constructor by default.
When copying a large number of objects, nrv can optimize the program and improve efficiency. However, you can think about the side effects of nrv, for example, in Visual Studio 2010, did you know how to call copy constructor? That is to say, a default constructor will be generated when the compiler feels appropriate, but I do not know what is appropriate. I think I should not know it because I do not know it, in order to lay a hidden risk for our future programming. For example, your class has a static variable that records the number of times this object was copied. Due to this risk, it is likely that the results generated for the program are inconsistent with the expected ones. Meanwhile, because of nrv, the constructor won't be called either. Sometimes, we expect some statements in copy constructor to be called, but if nrv exists, it won't be possible.
Therefore, the need for nrv depends on your situation.

In Neusoft-Dalian

========================================================== ==================================

If you like it, follow these steps:Jellythink | idea jelly

More original highlightsWww.jellythink.com

You can also follow Sina Weibo:Http://weibo.com/u/1887014677

========================================================== ==================================

Related Article

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.