473,799 Members | 2,999 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
14 2198
George2
200 New Member
Hi weaknessforcats ,


I agree it is not the same object instance, but each different object is doing similar things, making a temporary object on the function return stack and assign it to the return value, then destruct the temporary object on function stack. This is the area which NRVO covers, why it could not optimize, I do not know.

It is appreciated if you could share some more perpectives. :-)

NRVO does not optimize if there are multiple return paths because it's not the same object every time.

regards,
George
Dec 31 '07 #11
weaknessforcats
9,208 Recognized Expert Moderator Expert
You said it yourself: each different object.

NRVO is Named Returned Value Object. Not Objects.

You have to have only one object be the named return value.
Jan 1 '08 #12
George2
200 New Member
Hi weaknessforcats ,


I am confused. Named Return Value Optimization, is not the same as only one named return value optimization.

You can test that if you have two different variable instances, different names for different return paths, NRVO still does not work.

I am not sure why there is such limitation. I am not sure whether I have missed or mis-understood some important aspects of NRVO, which blocks my understanding of why multiple return names with multiple return paths blocks compiler from optimization. Any ideas?

You said it yourself: each different object.

NRVO is Named Returned Value Object. Not Objects.

You have to have only one object be the named return value.

regards,
George
Jan 2 '08 #13
weaknessforcats
9,208 Recognized Expert Moderator Expert
Any ideas?
You can research the C++ language spec. Once you find that multiple return pathes prevent NRVO, then you can try to find the ANSI committee C++ design jnotes where that conclusion was reached.

You will never get to the bottom of this and what you think about it doesn't count. You need to find hard factual documentation to support your position. Nothing I can say is going to help.
Jan 2 '08 #14
George2
200 New Member
Thanks all the same, I appreciate your help all the way.

You can research the C++ language spec. Once you find that multiple return pathes prevent NRVO, then you can try to find the ANSI committee C++ design jnotes where that conclusion was reached.

You will never get to the bottom of this and what you think about it doesn't count. You need to find hard factual documentation to support your position. Nothing I can say is going to help.

regards,
George
Jan 3 '08 #15

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
1523
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
1988
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
1752
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
7338
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
1162
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
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10491
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
10268
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
10031
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...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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
5467
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
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.