R
rwf_20
Hi,
I'm looking at the differences between:
const NonTrivialObject& obj =
functionThatReturnsANonTrivialObjectByValue();
and:
const NonTrivialObject obj =
functionThatReturnsANonTrivialObjectByValue();
My question is, can I expect these two lines to be any different?
I assumed that the object returned by the function becomes a
temporary. In the first line, I thought that the const reference
would be bound to that temporary, causing the temporary to hang around
until the reference dies.
In the second line, I assumed that the temporary returned from the
function is then copied into 'obj'. Hence, the first line saves a
copy. My testing has proven this completely wrong. See the following
code and results. Is there anything in the standard that could
clarify this, or is this completely compiler-specific?
The code below compiled in gcc 3.4.4 outputs:
Start.
Copy!
A
B
C
Copy!
D
E
F
Microsoft CL 14.00.5 (from VS 8.0) outputs:
Start.
Copy!
A
B
Copy!
C
Copy!
D
E
Copy!
F
Code:
#include <iostream>
class testClass {
public:
testClass() { }
testClass(const testClass& t) { std::cerr << "Copy!\n"; }
};
class ObjectFactory {
public:
ObjectFactory() { }
testClass getResidentObject() const {
return m_object;
}
testClass getLocalTempObject() {
return testClass();
}
testClass getLocalObject() {
testClass t;
return t;
}
private:
testClass m_object;
};
int main() {
ObjectFactory of;
std::cerr << "Start.\n";
const testClass& t1 = of.getResidentObject();
std::cerr << "A\n";
const testClass& t2 = of.getLocalTempObject();
std::cerr << "B\n";
const testClass& t3 = of.getLocalObject();
std::cerr << "C\n";
const testClass t4 = of.getResidentObject();
std::cerr << "D\n";
const testClass t5 = of.getLocalTempObject();
std::cerr << "E\n";
const testClass t6 = of.getLocalObject();
std::cerr << "F\n";
return 0;
}
Any insight would be appreciated. Basically, I'm trying to decide if
it's ever worth using a const reference to refer to something returned
from a function.
Ryan
I'm looking at the differences between:
const NonTrivialObject& obj =
functionThatReturnsANonTrivialObjectByValue();
and:
const NonTrivialObject obj =
functionThatReturnsANonTrivialObjectByValue();
My question is, can I expect these two lines to be any different?
I assumed that the object returned by the function becomes a
temporary. In the first line, I thought that the const reference
would be bound to that temporary, causing the temporary to hang around
until the reference dies.
In the second line, I assumed that the temporary returned from the
function is then copied into 'obj'. Hence, the first line saves a
copy. My testing has proven this completely wrong. See the following
code and results. Is there anything in the standard that could
clarify this, or is this completely compiler-specific?
The code below compiled in gcc 3.4.4 outputs:
Start.
Copy!
A
B
C
Copy!
D
E
F
Microsoft CL 14.00.5 (from VS 8.0) outputs:
Start.
Copy!
A
B
Copy!
C
Copy!
D
E
Copy!
F
Code:
#include <iostream>
class testClass {
public:
testClass() { }
testClass(const testClass& t) { std::cerr << "Copy!\n"; }
};
class ObjectFactory {
public:
ObjectFactory() { }
testClass getResidentObject() const {
return m_object;
}
testClass getLocalTempObject() {
return testClass();
}
testClass getLocalObject() {
testClass t;
return t;
}
private:
testClass m_object;
};
int main() {
ObjectFactory of;
std::cerr << "Start.\n";
const testClass& t1 = of.getResidentObject();
std::cerr << "A\n";
const testClass& t2 = of.getLocalTempObject();
std::cerr << "B\n";
const testClass& t3 = of.getLocalObject();
std::cerr << "C\n";
const testClass t4 = of.getResidentObject();
std::cerr << "D\n";
const testClass t5 = of.getLocalTempObject();
std::cerr << "E\n";
const testClass t6 = of.getLocalObject();
std::cerr << "F\n";
return 0;
}
Any insight would be appreciated. Basically, I'm trying to decide if
it's ever worth using a const reference to refer to something returned
from a function.
Ryan