PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > why visual studio does not optimize constructor in this case
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
why visual studio does not optimize constructor in this case

Réponse
 
LinkBack Outils de la discussion
Vieux 27/12/2007, 13h27   #1
George2
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/lib...57(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
  Réponse avec citation
Vieux 27/12/2007, 13h49   #2
Pavel Shved
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why visual studio does not optimize constructor in this case

On 27 ÄÅË, 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 = 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!
  Réponse avec citation
Vieux 27/12/2007, 15h23   #3
Tadeusz B. Kopec
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why visual studio does not optimize constructor in this case

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/lib...57(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.

--
Tadeusz B. Kopec (tkopec@NOSPAMPLEASElife.pl)
Sailors in ships, sail on! Even while we died, others rode out the storm.
  Réponse avec citation
Vieux 27/12/2007, 18h15   #4
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why visual studio does not optimize constructor in this case

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/lib...57(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éponse avec citation
Vieux 28/12/2007, 00h44   #5
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why visual studio does not optimize constructor in this case

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h16.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11268 seconds with 13 queries