Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy constructor and return value optimization

Reply
Thread Tools

copy constructor and return value optimization

 
 
Rahul
Guest
Posts: n/a
 
      07-05-2008
While reading "Efficient C++" by Dov Bulka I came across the follown
statement
"In addition, you must also define a copy constructor to "turn on" the
Return Value Optimization(RVO). If the
class involved does not have a copy constructor defined, the RVO is
quietly turned off."

I searched a lot on the groups but still could not find any
satisfactory answer which explains this.
My doubt is "Even if we don't define a copy constructor then compiler
defines one automatically for us". So in this case also compiler
should do optimization. Then why does the book says "you must define"


I know if copy constructor is provate then you can not write
statements like
Complex c = c1+ c1;
which means compiler has no oppertunity to apply RVO.

But I am confused with the case when "we have compiler provided copy
constructor". When i checked with VC++ 7 then it did RVO with the
compiler generated copy constructor also.

Please let me know if i mis-interperted the book's statement, or
please explain me the above case.

Regards

 
Reply With Quote
 
 
 
 
Thomas Grund
Guest
Posts: n/a
 
      07-07-2008
On Sat, 5 Jul 2008, Rahul wrote:

> While reading "Efficient C++" by Dov Bulka I came across the follown
> statement
> "In addition, you must also define a copy constructor to "turn on" the
> Return Value Optimization(RVO). If the
> class involved does not have a copy constructor defined, the RVO is
> quietly turned off."
>
> I searched a lot on the groups but still could not find any
> satisfactory answer which explains this.
> My doubt is "Even if we don't define a copy constructor then compiler
> defines one automatically for us". So in this case also compiler
> should do optimization. Then why does the book says "you must define"
>
>
> I know if copy constructor is provate then you can not write
> statements like
> Complex c = c1+ c1;
> which means compiler has no oppertunity to apply RVO.
>
> But I am confused with the case when "we have compiler provided copy
> constructor". When i checked with VC++ 7 then it did RVO with the
> compiler generated copy constructor also.
>
> Please let me know if i mis-interperted the book's statement, or
> please explain me the above case.
>
> Regards
>
>



Hi,

My experience is that in some cases you must define even trivial copy
constructors or assignment operators by hand (only for efficiency
reasons).

The following example is 3 to 4 times faster (VC71) if you uncomment the
copy constructor and =operator.

For explanations please ask again or take a look at the generated
assembler code.

Thomas




#include <iostream>

struct Vector3
{
Vector3(double a, double b, double c)
{
d[0] = a; d[1] = b; d[2] = c;
}

/*
Vector3(const Vector3 & o)
{
d[0] = o.d[0]; d[1] = o.d[1]; d[2] = o.d[2];
}

void operator=(const Vector3 & o)
{
d[0] = o.d[0]; d[1] = o.d[1]; d[2] = o.d[2];
}
*/
double d[3];
};

Vector3 operator+(const Vector3 & a, const Vector3 & b)
{
return Vector3(a.d[0]+b.d[0],a.d[1]+b.d[1],a.d[2]+b.d[2]);
}

int main()
{
Vector3 a(1,2,3);
Vector3 b(0,0,0);

for (int i=0; i<1000000000; i++)
b=b+a;

std::cout << b.d[0] << " " << b.d[1] << " " << b.d[2] << '\n';
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
Copy constructor, return by value and const references Jan Lellmann C++ 7 11-09-2009 01:09 PM
CPP Output Question - related to copy-constructor and return by value sanjay C++ 1 10-09-2008 01:16 PM
Is a copy constructor needed for the return value optimization? SzH C++ 10 04-26-2007 02:45 PM
copy constructor and return by value bipod.rafique@gmail.com C++ 3 07-29-2005 02:49 PM



Advertisments