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 -
#include <stdio.h>
-
class RVO
-
{
-
public:
-
-
RVO(){printf("I am in constructor\n");}
-
RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");}
-
int mem_var;
-
};
-
RVO MyMethod (int i)
-
{
-
RVO rvo;
-
rvo.mem_var = i;
-
if (rvo.mem_var == 10)
-
return (RVO());
-
return (rvo);
-
}
-
int main()
-
{
-
RVO rvo;
-
rvo=MyMethod(5);
-
}
-
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
14 2198
Maybe the compiler would optimize if you wrote (in main)
With two lines, the compiler has no other choice then to use the default constructor, then replace with the copy constructor.
This is the output I would expect:
I am in constructor
I am in constructor
I am in copy constructor
because in main():
has to create an object. Threrefore: I am in constructor
Then in this call: -
RVO MyMethod (int i)
-
{
-
RVO rvo;
-
}
-
has to create an object. Threrefore: I am in constrcutor
Then the object has to be returned: -
RVO MyMethod (int i)
-
{ //etc...
-
return (rvo);
-
}
-
That means a copy. Therefore: I am in copy constrcutor.
Finally, in main():
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.
Thanks chroot,
Maybe the compiler would optimize if you wrote (in main)
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
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
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:
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: -
RVO rvo=MyMethod(5);
-
rvo=MyMethod(5);
-
The question is: How many assignments are there. When you answer 2, you have failed the interview.
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:
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: -
RVO rvo=MyMethod(5);
-
rvo=MyMethod(5);
-
The question is: How many assignments are there. When you answer 2, you have failed the interview.
regards,
George
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.
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
NRVO does not optimize if there are multiple return paths because it's not the same object every time.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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>
|
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
|
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.
...
|
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;
| |
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...
|
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 {
|
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
|
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...
|
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...
|
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...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |