Re: return type of operator+()
On 18 Sep., 11:13, Scooser wrote:
you cannot write (a + b) = c but also c = (a + b).someNonConstMethod(=
)
is not possible any longer.
Right. In C++0x a better approach would be to limit assignment to
lvalues instead of using const return values. This will make
(a+b)=c;
ill-formed but still allow
(a+b).someNonConstFunction()
if someNonConstFunction has no lvalue-ref-qualifier. Also, it allows
moving resources from (non-const) temporary objects to other objects
(move semantics). For small objects that only contain an integer this
won't matter. But in other cases like std::valarray it will make a
difference. An assignment operator restricted to lvalues will look
like this:
class Test {
...
public:
Test& operator=(Test const&) & ;
// note the ref qualifier: ^^^
...
};
In the light of C++0x, move constructors and ref qualifiers, the habit
of returning const objects is probably going to disappear. Even
without C++0x I don't return const objects. I just don't consider (a+b)
=c to be a problem.
btw. why creating the temp object yourself? Let the compiler do this
for you.
inline const Test operator+ (Test lhs, const Test& rhs)
{
return lhs += rhs;
}
Before recommending something like this, please make sure that it is
actually an improvement over what the OP wrote. The problem here is
that no compiler I know of will be able to apply RVO (return value
optimization) in this case. For two reasons, actually. Firstly: It's a
parameter. Secondly: the return type of operator+= is an lvalue
reference. How would the compiler know what this reference refers to
without inlining the operator+= function? The compilers I tested are
not able to figure that out even with an inline operator+= and
optimizations enabled.
Assuming your compiler can apply URVO and NRVO except for parameters
(which is the behaviour of both Microsoft's and GNU's C++ compiler as
far as I know) and further assuming that your compiler can elide a
copy if the argument was an rvalue we get the following results:
1st arg | subramanian | Scooser
--------+-------------+--------
lvalue | 1 | 2
rvalue | 1 | 1
Table 1: Count of copy constructions
The one copy in subramanian's is the copy he created manually. NRVO
applies. RVO is not applied in Scooser's version at all. But in case
of rvalue arguments the copy can be elided which is why it also uses
only one copy construction. But for lvalue arguments two copy
constructions are used.
Cheers,
SG