By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

Nov 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* du********@gmail.com:
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?
Nov 10 '06 #2

P: n/a

du********@gmail.com 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

Nov 10 '06 #3

P: n/a

<du********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr 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
Nov 10 '06 #4

P: n/a
On 9 Nov 2006 20:58:52 -0800, du********@gmail.com 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
Nov 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.