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

D

duffdevice

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


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
A

Alf P. Steinbach

* (e-mail address 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.
 
S

Salt_Peter

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
 
R

Roland Pibinger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top