why visual studio does not optimize constructor in this case

G

George2

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);
}

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
 
P

Pavel Shved

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 = 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!
 
T

Tadeusz B. Kopec

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);
}

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.
 
S

Salt_Peter

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);}

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
*/
 
R

Ron Natalie

George2 said:
}
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top