469,568 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

copy constructor and return value optimization

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

Jul 5 '08 #1
1 1678
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';
}
Jul 7 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by I wish | last post: by
3 posts views Thread by bipod.rafique | last post: by
3 posts views Thread by Tony Johansson | last post: by
3 posts views Thread by Igor.Smirnov | last post: by
9 posts views Thread by janzon | last post: by
15 posts views Thread by subramanian100in | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.