Re: why visual studio does not optimize constructor in this case
Cool Dave,
1.
I have tried and the output is,
--------------------
I am in constructor
I am in copy constructor
--------------------
Why compiler could optimize this case? As stated in MSDN, if using different
return variable name, compiler should not be able to optimize.
2.
This is my perpective mentioned before, it is also appreciated if you could
review and comment. :-)
--------------------
I do not know why which rvo instance will return matters. I think no matter
which one will return, either rvo or RVO(), the return value is a temporary
obejct, and compiler could optimize it by using assignment operator on the
outside rvo object instance directly without creating the temporary object,
right?
Why the path matters? Could you provide more information about your analysis
please?
--------------------
regards,
George
"David Webber" wrote:
"George" <George@discussions.microsoft.com> wrote in message
news:A6DC143F-A48F-4970-9E79-5A2528DA280C@microsoft.com...
#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);
}
[/Code]
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
The line in main:
RVO rvo; // in constructor
The line in MyMethod:
RVO rvo; // in constructor
the line in MyMethod:
return (rvo);
places a *copy* of rvo on the stack
where main can get at it.
Why not try instead
RVO rvo=MyMethod(5);
and see what you get :-)
Alternatively step through with the debugger and see where it goes into the
constructors.
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm