473,788 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why visual studio does not optimize constructor in this case

200 New Member
Hello everyone,


Why visual studio does not optimize constructor in this case? I do not understand what the MSDN mentioned,

if use different named object, compiler can not optimize. Why?

http://msdn2.microsoft .com/en-us/library/ms364057(vs.80) .aspx

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. class RVO
  3. {
  4. public:
  5.  
  6.             RVO(){printf("I am in constructor\n");}
  7.             RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");}
  8.             int mem_var;       
  9. };
  10. RVO MyMethod (int i)
  11. {
  12.             RVO rvo;
  13.             rvo.mem_var = i;
  14.       if (rvo.mem_var == 10)
  15.          return (RVO());
  16.             return (rvo); 
  17. }
  18. int main()
  19. {
  20.             RVO rvo;
  21.             rvo=MyMethod(5);
  22. }
  23.  
Output is,

I am in constructor
I am in constructor
I am in copy constructor

My expected output is,

I am in constructor
I am in constructor


thanks in advance,
George
Dec 27 '07 #1
14 2196
chroot
13 New Member
Maybe the compiler would optimize if you wrote (in main)

Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
With two lines, the compiler has no other choice then to use the default constructor, then replace with the copy constructor.
Dec 27 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
This is the output I would expect:

I am in constructor
I am in constructor
I am in copy constructor

because in main():
Expand|Select|Wrap|Line Numbers
  1. RVO rvo;
  2.  
has to create an object. Threrefore: I am in constructor
Then in this call:
Expand|Select|Wrap|Line Numbers
  1. RVO MyMethod (int i)
  2. {
  3.             RVO rvo;
  4. }
  5.  
has to create an object. Threrefore: I am in constrcutor

Then the object has to be returned:
Expand|Select|Wrap|Line Numbers
  1. RVO MyMethod (int i)
  2. {     //etc...
  3.       return (rvo); 
  4. }
  5.  
That means a copy. Therefore: I am in copy constrcutor.

Finally, in main():
Expand|Select|Wrap|Line Numbers
  1. rvo=MyMethod(5);
  2.  
Normally, the copy constructor would be called to create an initialize rvo. However, the optimization (NRVO) allows the compiler to keep the returned object and call it rvo, thereby saving a constrcutor call.

Beware that the temporary object returned from MyMethod() does not contain any local pointers or references.
Dec 27 '07 #3
George2
200 New Member
Thanks chroot,


Maybe the compiler would optimize if you wrote (in main)

Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
With two lines, the compiler has no other choice then to use the default constructor, then replace with the copy constructor.
I have tried that when using the alternative method you mentioned, the output is,

--------------------
I am in constructor
I am in copy constructor
--------------------

which means it is optimized, cool!

What do you mean *the compiler has no other choice then to use the default constructor, then replace with the copy constructor*? Could you provide more information please? I am interested to learn from you why using your alternative method will trigger compiler to optimize the code to overcome MSDN sample. :-)


regards,
George
Dec 28 '07 #4
George2
200 New Member
Thanks weaknessforcats ,


I do not quite understand why compiler could not optimize the case. The object returned from MyMethod() is temporary, and we could invoke the assignment operator the outer rvo, which could save one time to call copy constructor.

Why compiler can not optimize in this case, could you provide more description please?

Normally, the copy constructor would be called to create an initialize rvo. However, the optimization (NRVO) allows the compiler to keep the returned object and call it rvo, thereby saving a constrcutor call.

Beware that the temporary object returned from MyMethod() does not contain any local pointers or references.

regards,
George
Dec 28 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
do not quite understand why compiler could not optimize the case. The object returned from MyMethod() is temporary, and we could invoke the assignment operator the outer rvo, which could save one time to call copy constructor.
You can't use the assignment operator on a object that's not built yet.

This is not an assignment:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
I know, I know, you see the assignment operator but this is not an assignment. It's an initialization. Initialization requires a constructor. NRVO just lets you use the temporary object retured from MyMethod as rvo.

BTW: This is in every interview I have ever had:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2. rvo=MyMethod(5);
  3.  
The question is: How many assignments are there. When you answer 2, you have failed the interview.
Dec 28 '07 #6
George2
200 New Member
I agree with you, weaknessforcats . Let us come back to the original question. :-)


http://msdn2.microsoft .com/en-us/library/ms364057(vs.80) .aspx

You can compare sample 1 and sample 4. The only differences as I stated in my original question is, sample 4 uses different return paths and this is the root cause why compiler can not use NRVO to optimize. I do not know and understand why different return paths matters whether or not compiler will do the optimization. Any ideas?

You can't use the assignment operator on a object that's not built yet.

This is not an assignment:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
I know, I know, you see the assignment operator but this is not an assignment. It's an initialization. Initialization requires a constructor. NRVO just lets you use the temporary object retured from MyMethod as rvo.

BTW: This is in every interview I have ever had:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2. rvo=MyMethod(5);
  3.  
The question is: How many assignments are there. When you answer 2, you have failed the interview.

regards,
George
Dec 29 '07 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
I do not know and understand why different return paths matters whether or not compiler will do the optimization. Any ideas?
Code has been generated to return objects from each of the return paths. Which one should be the Named Returned Value?? Path #1, Path #2 or Path #3 ??? In any ambiguous situation, the compiler backs off optimization as unsafe.

Now I suppose we could philosophize on this but the fact is that's how it works. Other things prevent NRVO and they are in that MSDN topic as well.
Dec 29 '07 #8
George2
200 New Member
Hi weaknessforcats ,


I do not understand why different return paths will block compiler from optimization, for each path #1, #2 or #3, compiler could optimize separately by using hidden argument technique, because for each return path, without optimization they all need to create temporary object on return stack.

Any ideas why compiler could not optimize?

Code has been generated to return objects from each of the return paths. Which one should be the Named Returned Value?? Path #1, Path #2 or Path #3 ??? In any ambiguous situation, the compiler backs off optimization as unsafe.

Now I suppose we could philosophize on this but the fact is that's how it works. Other things prevent NRVO and they are in that MSDN topic as well.

regards,
George
Dec 30 '07 #9
weaknessforcats
9,208 Recognized Expert Moderator Expert
NRVO does not optimize if there are multiple return paths because it's not the same object every time.
Dec 30 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
6178
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with itself. Everything you need is available at no costs (except download hassle and installation time). Once your system is set up properly its just a matter of running 'python setup.py build'. No longer waiting for someone else to build binaries and a...
19
3558
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours to find the _current_ reporting page... #include <vector> #include <iostream>
1
1522
by: jmd | last post by:
Hello. I have downloaded and installed Visual Studio 2005 February CTP -- Professional Edition (English) from msdn. All works fine (with a few bugs) except the Quickstarts. The Quickstarts install without problems. I have 2 questions : 1. When I launch the Quickstarts, I only see : -------------------------------------------------------------------------------- Microsoft .NET Framework SDK QuickStart Tutorials
8
1987
by: Will Chamberlain | last post by:
I came across a rather interesting article this morning and thought I'd share. We all know that Visual Studio is a great IDE, but I think we can all agree that it is adds a dramatic change to how we write code. I'm not posting to talk trash or start a flamewar, just wanting feedback in regards to the following article. I happen to use Visual Studio on a daily basis and am not a John Rivers alter-ego. ...
4
1751
by: pascalroca | last post by:
class myClass { int lacement ; public : myClass(){}; myClass(const myClass& ref){} virtual std::vector< std::auto_ptr< myClass getValeur() = 0; }; typedef std::auto_ptr< myClassmyClassPtr;
3
1374
by: karlag92 | last post by:
We have a very large C# winforms client application that is constructed as a single solution currently with 75 projects. We're currently using VS 2003 but will upgrade to 2005 some time next year. We've noticed that Visual Studio is terribly inefficient about a lot of things, and my testing with 2005 seems to indicate those problems may actually be worse there. On average, it can take 3 to 5 minutes to simply open the solution while...
4
1180
by: George2 | last post by:
Hello everyone, Why Visual Studio compiler can not optimize in this case? I think this case is almost the same as sample 1, why compiler can optimize sample 1 but can not optimze sample 2? (sample 2, http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx) #include <stdio.h> class A {
4
376
by: George2 | last post by:
Hello everyone, Why visual studio does not optimize constructor in this case? I do not understand what the MSDN mentioned, if use different named object, compiler can not optimize. Why? http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx
0
7336
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results. Visual Studio automatically creates a number of...
2
1160
Markus
by: Markus | last post by:
I recently installed Visual Studio 2008 Professional. On the initial install it gave me a list of options, something to do with optimizing Visual Studio. It gave you the option to optimize it for C#, J#, basically whatever you primarily worked with. I selected C#. It did give you an option to utilise all of the projects, but I silly neglected that. Now, when I go to create a new project I'm only able to create C# projects. The problem is that...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.