Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 04:45 AM
duffdevice@gmail.com
Guest
 
Posts: n/a
Default Please explain this unexpected behavior (regarding return-by-value)


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)?
*/


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

  #2  
Old November 10th, 2006, 04:55 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Please explain this unexpected behavior (regarding return-by-value)

* duffdevice@gmail.com:
Quote:
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?
  #3  
Old November 10th, 2006, 05:15 AM
Salt_Peter
Guest
 
Posts: n/a
Default Re: Please explain this unexpected behavior (regarding return-by-value)


duffdevice@gmail.com wrote:
Quote:
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

  #4  
Old November 10th, 2006, 08:45 AM
*PaN!*
Guest
 
Posts: n/a
Default Re: Please explain this unexpected behavior (regarding return-by-value)


<duffdevice@gmail.comwrote in message
news:1163134732.131887.307810@m73g2000cwd.googlegr oups.com...
Quote:
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


  #5  
Old November 10th, 2006, 08:55 AM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: Please explain this unexpected behavior (regarding return-by-value)

On 9 Nov 2006 20:58:52 -0800, duffdevice@gmail.com wrote:
Quote:
>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
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles