Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Please explain this unexpected behavior (regarding return-by-value)

Reply
Thread Tools

Please explain this unexpected behavior (regarding return-by-value)

 
 
duffdevice@gmail.com
Guest
Posts: n/a
 
      11-10-2006

Hi, I came across this unexpected behavior while working on something
else. I am attempting to return a custom type by value from a global
function. I have a trace in the custom class's copy constructor, and I
expected this code to tell me that the custom copy constructor had been
called twice. Instead, it's only called once and the program executes
correctly.

Is this an optimization related to my compiler (g++ v4.0.1) or is this
specified by the standard? Can somebody clear this behavior up for me?

Thanks in advance!

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// start example source
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;

class Test
{
string member_;

public:
Test(const string& s) : member_(s) { }
Test(const Test& copy) : member_(copy.member_) {
cout << "copy" << endl;
}

void out() { cout << member_ << endl; }

};

Test test_by_value(const Test& in)
{

Test temp(in); // expect a copy here while constructing temp
return temp; // also expect a copy here to return the value of
temp, which currently exists on the stack
}

int main()
{
Test t("hello");
Test v = test_by_value(t);
v.out();

return 0;
}


/*
expected output:
copy
copy
hello


actual output:
copy
hello

how is the copied value being returned from test_by_value() without a
second copy (since temp is a stack var)?
*/


//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-10-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> Hi, I came across this unexpected behavior while working on something
> else. I am attempting to return a custom type by value from a global
> function. I have a trace in the custom class's copy constructor, and I
> expected this code to tell me that the custom copy constructor had been
> called twice. Instead, it's only called once and the program executes
> correctly.


Copy constructor calls are allowed to be optimized away -- in many
situations -- regardless of possible side-effects in the copy constructor.

So generally you can't "expect" any specific number of calls.

For your specific example, look up RVO and NRVO.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-10-2006

(E-Mail Removed) wrote:
> Hi, I came across this unexpected behavior while working on something
> else. I am attempting to return a custom type by value from a global
> function. I have a trace in the custom class's copy constructor, and I
> expected this code to tell me that the custom copy constructor had been
> called twice. Instead, it's only called once and the program executes
> correctly.
>
> Is this an optimization related to my compiler (g++ v4.0.1) or is this
> specified by the standard? Can somebody clear this behavior up for me?
>
> Thanks in advance!
>
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////
> // start example source
> #include <string>
> #include <iostream>
> using std::string;
> using std::cout;
> using std::endl;
>
> class Test
> {
> string member_;
>
> public:
> Test(const string& s) : member_(s) { }
> Test(const Test& copy) : member_(copy.member_) {
> cout << "copy" << endl;
> }
>
> void out() { cout << member_ << endl; }
>
> };
>
> Test test_by_value(const Test& in)
> {
>
> Test temp(in); // expect a copy here while constructing temp
> return temp; // also expect a copy here to return the value of
> temp, which currently exists on the stack
> }
>
> int main()
> {
> Test t("hello");
> Test v = test_by_value(t);
> v.out();
>
> return 0;
> }
>
>
> /*
> expected output:
> copy
> copy
> hello
>
>
> actual output:
> copy
> hello
>
> how is the copied value being returned from test_by_value() without a
> second copy (since temp is a stack var)?
> */



Because the following:

Test v = test_by_value(t);

is usually optimized to:

Test v(test_by_value(t));

which is completely legal, btw.

Note the same happens with a primitive:
int x(99);
int n = x; // is actually a copy

 
Reply With Quote
 
*PaN!*
Guest
Posts: n/a
 
      11-10-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> Is this an optimization related to my compiler (g++ v4.0.1) or is this
> specified by the standard? Can somebody clear this behavior up for me?


VS2005 does a similar optimization, the NRVO
(http://msdn.microsoft.com/library/de...rvo_cpp05.asp).

--
Marco


 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      11-10-2006
On 9 Nov 2006 20:58:52 -0800, (E-Mail Removed) wrote:
>Is this an optimization related to my compiler (g++ v4.0.1) or is this
>specified by the standard? Can somebody clear this behavior up for me?


This 'optimization' (the (N)RVO hack) is compiler specific. IIRC, you
can turn it off with a compiler switch for g++.

Best wishes,
Roland Pibinger
 
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
Can someone explain this unexpected raw_input behavior? Mike Kent Python 9 01-24-2008 11:18 PM
Please Explain Strange Dial-Up Modem Behavior Way Back Jack Computer Support 3 08-19-2007 02:45 AM
Please explain strange viewstate behavior... =?Utf-8?B?cmdyYW5kaWRpZXI=?= ASP .Net 1 10-27-2005 08:57 AM
please explain why this is Undefined Behavior REH C++ 25 03-29-2005 10:19 PM
unexpected behavior in Datagrid paging : PLEASE HELP >: ( Juan Romero ASP .Net Datagrid Control 1 09-11-2003 04:38 AM



Advertisments