473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1879
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: I wish | last post by:
I wrote some program #include <iostream> class A { public: A() {} A( const A& t ) { std::cout << "Good" << std::endl; } }; A f()
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
3
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
3
by: Igor.Smirnov | last post by:
Hi, Why g++ in Linux does not call copy constructor at return statement? I have detected this at Sicentific Linux 4.1 (g++ v. 3.4.3) and Red Hat Linux 8.0 (g++ v. 3.2). Microsoft Visual...
9
by: janzon | last post by:
Consider the code below. The output is the following two lines: 0xbfc78090 0xbfc780a0 This proves that the variable m in main() is not the very same instance of MyClass as temp_m in hello()....
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
10
by: SzH | last post by:
The code below demonstrates that the copy constructor of moo is not called on the first line of main. In spite of this, g++ (version 4.1.2) refuses to compile it if I make the copy constructor...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
15
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class Test { public: Test(int xx) : x(xx) { cout << x << endl; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.