why visual studio does not optimize constructor in this case



 DEVELOP > c-Plus-Plus > why visual studio does not optimize constructor in this case

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "George2"
Date: 27 Dec 2007 07:27:31 AM
Object: why visual studio does not optimize constructor in this case
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
[Code]
#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
thanks in advance,
George
.

User: "Ron Natalie"

Title: Re: why visual studio does not optimize constructor in this case 27 Dec 2007 06:44:36 PM
George2 wrote:

}
int main()
{
RVO rvo;
rvo=MyMethod(5);
}
[/Code]

It's not allowed to.
If you had said
RVO rvo = MyMethod(5)
it would have been a different story.
Your default constructor has side effects and the compiler isn't
justified in omitting it.
What RVO allows for is the elision of COPIED objects. It can't
elide the default constructed object which you later assign into.
.

User: "Pavel Shved"

Title: Re: why visual studio does not optimize constructor in this case 27 Dec 2007 07:49:28 AM
On 27 =C4=C5=CB, 16:27, George2 <george4acade...@yahoo.com> wrote:

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?

Because compiler is not as clever as you.
Optimization is done (most likely) by determining that local variable
rvo in MyMethod() is an alias to rvo in main(). After compiler
determined that he will compile the code as doing the operations with
rvo within MyMethod in the memory location that main::rvo is in. But
to *compile* such code compiler must be sure that you always, from the
very beginning of MyMethod will operate with the thing you will
return.
In more simple words (which should be said due to lack of english
knowledge :-D) compiler can not determine what rvo is aliased to in
the following line:

rvo.mem_var =3D i;

if you return RVO() then rvo in the line mentioned is aliased to local
object. If you return local rvo, it's aliased to external main::rvo.
Which should compiler choose - and which *you* would like to? So it
should be (a) known in compile-time and (b) rules must be clear to
programmer. So it's just like stated in the beginning: for such code
optimization doesnt take place.
P.S. Of course, when im talking about `aliasing' i don't mean language
concepts, just a common word.
P.P.S. Happy New Year!
.

User: "Salt_Peter"

Title: Re: why visual studio does not optimize constructor in this case 27 Dec 2007 12:15:39 PM
On Dec 27, 8:27 am, George2 <george4acade...@yahoo.com> wrote:

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

[Code]
#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

thanks in advance,
George

The function above has two return paths, one returns an object and the
other invokes a constructor.
So what campiler X does in such a situation is not C++'s concern.
Here's the same with a single ctor invoked:
#include <cstdio>
class RVO
{
public:
RVO() : mem_var(0)
{
printf("I am in constructor\n");
}
RVO( const int n ) : mem_var(n)
{
printf("I am in parametized constructor\n");
}
RVO (const RVO& c_RVO)
{
printf("I am in copy constructor\n");
}
int mem_var;
};
RVO MyMethod (int i)
{
if(10 == i)
return RVO();
else
return RVO(i);
}
int main()
{
RVO rvo = MyMethod(5);
}
/*
I am in parametized constructor
*/
.

User: "Tadeusz B. Kopec"

Title: Re: why visual studio does not optimize constructor in this case 27 Dec 2007 09:23:22 AM
On Thu, 27 Dec 2007 05:27:31 -0800, George2 wrote:

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

[Code]
#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 standard says that compiler is allowed to elide copy in return value
but doesn't require it. Whether a specific compiler does this
optimisation or not is implementation defined so off topic here. As I can
understand the link you gave, VS requires either only one return
statement in function or returning same object (same variable?) with no
destructor in all return statements, to perform this optimisation.
--
Tadeusz B. Kopec (tkopec@NOSPAMPLEASElife.pl)
Sailors in ships, sail on! Even while we died, others rode out the storm.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER