|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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);
}
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 */ |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|