473,320 Members | 1,916 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,320 software developers and data experts.

Is a copy constructor needed for the return value optimization?

SzH

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 private. But the
Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this restriction
exist?)

Szabolcs

#include <iostream>

struct moo {
int data;

moo(int n) : data(n) {
std::cout << "constructor\n";
}

// private:
moo(const moo &m) : data(m.data) {
std::cout << "copy constructor\n";
}
};

moo fun() {
return moo(3);
}

int main() {
moo m = fun();
return 0;
}
Apr 25 '07 #1
10 2938
SzH wrote:
>
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 private. But the
Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this restriction
exist?)
Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make valid
code fail to compile. Returning a result by value, as your function
does, requires copying the value, which in turn requires an accessible
copy constructor. When you make the copy constructor inaccessible you
cannot return an object of that type by value.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 25 '07 #2
SzH
Pete Becker wrote:
SzH wrote:
>>
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 private.
But the Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this
restriction exist?)

Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make valid
code fail to compile. Returning a result by value, as your function
does, requires copying the value, which in turn requires an accessible
copy constructor. When you make the copy constructor inaccessible you
cannot return an object of that type by value.
I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on whether
the optimisation is performed or not. When I run the program, the line
"copy constructor" is not printed, so I can tell that the optimisation
was performed (i.e. the value was not copied).
Apr 25 '07 #3
SzH wrote:
Pete Becker wrote:
>SzH wrote:
>>>
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 private.
But the Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this
restriction exist?)

Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make
valid code fail to compile. Returning a result by value, as your
function does, requires copying the value, which in turn requires an
accessible copy constructor. When you make the copy constructor
inaccessible you cannot return an object of that type by value.

I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on
whether the optimisation is performed or not.
The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor. BTW, RVO is just
about the only optimization the Standard actually defines. And it
has to do that explicitly to state the "regardless of side effects"
portion.
When I run the program,
the line "copy constructor" is not printed, so I can tell that the
optimisation was performed (i.e. the value was not copied).
So? It doesn't remove the requirement that the Standard sets forth.
The code is ill-formed _if_ the copy *cannot* be made should it be
necessary to make it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #4
SzH
Victor Bazarov wrote:
The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor. BTW, RVO is just
about the only optimization the Standard actually defines. And it
has to do that explicitly to state the "regardless of side effects"
portion.
Just to make sure that I understand this correctly:
So the Standard *allows* this optimisation, but it does not *require*
it. Is this right?

Could you please point me to the relevant section of the Standard?
Unfortunately I was unable to find it myself.
Apr 25 '07 #5
SzH wrote:
Pete Becker wrote:
>SzH wrote:
>>>
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 private.
But the Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this
restriction exist?)

Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make valid
code fail to compile. Returning a result by value, as your function
does, requires copying the value, which in turn requires an accessible
copy constructor. When you make the copy constructor inaccessible you
cannot return an object of that type by value.

I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on whether
the optimisation is performed or not. When I run the program, the line
"copy constructor" is not printed, so I can tell that the optimisation
was performed (i.e. the value was not copied).
That's a different issue: the compiler is allowed to skip the copy
constructor. That doesn't make the code without an accessible copy
constructor valid, though. As I said, the optimization doesn't turn
invalid code into valid code.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 25 '07 #6
SzH wrote:
Victor Bazarov wrote:
>The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor. BTW, RVO is just
about the only optimization the Standard actually defines. And it
has to do that explicitly to state the "regardless of side effects"
portion.

Just to make sure that I understand this correctly:
So the Standard *allows* this optimisation, but it does not *require*
it. Is this right?
Yes.
Could you please point me to the relevant section of the Standard?
Unfortunately I was unable to find it myself.
12.8/15

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #7
On Wed, 25 Apr 2007 14:39:07 -0400, "Victor Bazarov" wrote:
>SzH wrote:
>I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on
whether the optimisation is performed or not.

The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor.
This should be regarded as bug in the C++ Standard.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 25 '07 #8
Roland Pibinger wrote:
On Wed, 25 Apr 2007 14:39:07 -0400, "Victor Bazarov" wrote:
>SzH wrote:
>>I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on
whether the optimisation is performed or not.

The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor.

This should be regarded as bug in the C++ Standard.
I don't see any defect report on it. Have you submitted one?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #9
On Apr 25, 8:28 pm, SzH <szhorvat-nospample...@gmail.comwrote:
Pete Becker wrote:
SzH wrote:
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 private.
But the Digital Mars compiler does not complain.
Which compiler is right? (And if gcc is right, why does this
restriction exist?)
Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make valid
code fail to compile. Returning a result by value, as your function
does, requires copying the value, which in turn requires an accessible
copy constructor. When you make the copy constructor inaccessible you
cannot return an object of that type by value.
I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on whether
the optimisation is performed or not.
Right. It's not normal optimization; it's a special hack. On
the other hand, it does seem reasonable to expect copy
constructors to copy.
When I run the program, the line "copy constructor" is not
printed, so I can tell that the optimisation was performed
(i.e. the value was not copied).
Correct. The standard explicitly allows this.

It doesn't change the fact that conceptually, a copy was
required; the standard just allows the compiler to ignore
observable behavior that occurs in the copy constructor and the
destructor. For purposes of optimization.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 26 '07 #10
On Apr 25, 10:45 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Wed, 25 Apr 2007 14:39:07 -0400, "Victor Bazarov" wrote:
SzH wrote:
I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on
whether the optimisation is performed or not.
The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor.
This should be regarded as bug in the C++ Standard.
Sort of. It's a hack. It was felt that it wouldn't make a
difference in well written C++ code (which is certainly true),
and that the optimization was too important to take support for
poorly written C++ into account. It's certain, however, that it
places games with the C++ object model.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 26 '07 #11

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() {
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...
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...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
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; }
1
by: Rahul | last post by:
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.