可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
As I've understand, when overloading operator=, the return value should should be a non-const reference.
A& A::operator=( const A& )
{
// check for self-assignment, do assignment
return *this;
}
It is non-const to allow non-const member functions to be called in cases like:
( a = b ).f();
But why should it return a reference? In what instance will it give a problem if the return value is not declared a reference, let's say return by value?
It's assumed that copy constructor is implemented correctly.
回答1:
Not returning a reference is a waste of resources and a yields a weird design. Why do you want to do a copy for all users of your operator even if almost all of them will discard that value?
a = b; // huh, why does this create an unnecessary copy?
In addition, it would be surprising to users of your class, since the built-in assignment operator doesn't copy likewise
int &a = (some_int = 0); // works
回答2:
A good general advice when overloading operators is 'do as primitive types do', and the default behavior of assignment to a primitive type is that.
Not returning anything could be an option, to disable assignment inside other expressions if you feel the need, but returning a copy does not make sense at all: if the caller wants to make a copy they can make it out of the reference, if they do not need the copy there is no need to generate a temporary that is not needed.
回答3:
Cause f() can modify a. (we return a non-const reference)
If we return a value (a copy) of a, f() will modify the copy, not a
回答4:
I'm not sure how often you'd want to do it, but something like: (a=b)=c;
requires a reference to work.
Edit: Okay, there is a bit more to it than that. Much of the reasoning is semi-historical. There's more reason you don't want to return an rvalue than just avoiding an unnecessary copy into a temporary object. Using a (minor) variation on an example originally posted by Andrew Koenig, consider something like this:
struct Foo {
Foo const &assign(Foo const &other) {
return (*this = other);
}
};
Now, assume you're using an old version of C++ where assignment returned an rvalue. In that case, (*this=other);
will yield that temporary. You're then binding a reference to the temporary, destroying the temporary, and finally returning a dangling reference to the destroyed temporary.
Rules that have been enacted since (extending the life of a temporary used to initialize a reference) would at least mitigate (and might completely cure) this problem, but I doubt anybody re-visited this particular situation after those rules had been written. It was a bit like an ugly device driver that includes kludges to work around dozens of bugs in different versions and variations of the hardware -- it could probably be refactored and simplified, but nobody's quite sure when some seemingly innocuous change will break something that currently works, and ultimately nobody wants to even look at it if they can help it.
回答5:
If your assignment operator does not take a const reference parameter:
A& A::operator=(A&); // unusual, but std::auto_ptr does this for example.
or if the class A
has mutable members (reference count?), then it is possible that the assignment operator changes the object being assigned from as well as assigned to. Then if you had code like this:
a = b = c;
The b = c
assignment would occur first, and return a copy (call it b'
) by value instead of returning a reference to b
. When the a = b'
assignment is done, the mutating assignment operator would change the b'
copy instead of the real b
.
Another potential problem -- returning by value instead of by reference could cause slicing if you have virtual assignment operators. I'm not saying that's a good idea, but it could be a problem.
If you intend to do something like (a = b).f()
then you will want it to return by reference so that if f()
mutates the object, it is not mutating a temporary.
回答6:
In real code (i.e. not things like (a=b)=c
), returning a value is unlikely to cause any compile errors, but it is inefficient to return a copy because creating a copy can often be expensive.
You can obviously come up with situation where a reference is needed, but those rarely -- if ever -- come up in practice.
回答7:
This is Item 10 of Scott Meyers' excellent book, Effective C++. Returning a reference from operator=
is only a convention, but it's a good one.
This is only a convention; code that doesn't follow it will compile. However, the convention is followed by all the built-in types as well as by all the types in the standard library. Unless you have a good reason for doing things differently, don't.
回答8:
If you're worried that returning the wrong thing might silently cause unintended side effects, you could write your operator=()
to return void
. I've seen a fair bit of code that does this (I assume out of laziness or just not knowing what the return type should be rather than for 'safety'), and it causes few problems. The kind of expressions that need to use the reference normally returned by operator=()
are pretty rarely used, and almost always easy code an alternative for.
I'm not sure I'd endorse returning void
(in a code review I'd probably call it out as something you shouldn't do), but I'm throwing it out there as an option to consider if you want to not have to worry about how oddball uses of the assignment operator might be handled.
late edit:
Also, I should have originally mentioned that you can split the difference by having your operator=()
return a const&
- that will still permit assignment chaining:
a = b = c;
But will disallow some of the more unusual uses:
(a = b) = c;
Note that this makes the assignment operator have semantics similar to what it has in C, where the value returned by the =
operator is not an lvalue. In C++, the standard changed it so the =
operator returns the type of the left operand, so it is an lvalue, but as Steve Jessop noted in a comment to another answer, while that makes it so the compiler will accept
(a = b) = c;
even for built-ins, the result is undefined behavior for built-ins since a
is modified twice with no intervening sequence point. That problem is avoided for non-builtins with an operator=()
because the operator=()
function call is a sequence point.
回答9:
Returning by reference reduces the time of performing chained operations. E. g. :
a = b = c = d;
Let's see the actions which would be called, if operator=
returns by value.
- Copy assignment opertor= for
c
makes c
equal to d
and then creates temporary anonymous object (calls copy ctor). Let's call it tc
.
- Then operator= for
b
is called. Right hand side object is tc. Move assignment operator is called. b
becomes equal to tc
. And then function copies b
to temporary anonymous, let's call it tb
.
- The same thing happens again,
a.operator=
returns temporary copy of a
. After operator ;
all three temporary objects are destroyed
Altogether: 3 copy ctors, 2 move operators, 1 copy operator
Let's see what will change if operator= will return value by reference:
- Copy assignment operator is called.
c
becomes equal to d
, reference to lvalue object is returned
- The same.
b
becomes equal to c
, reference to lvalue object is returned
- The same.
a
becomes equal to b
, reference to lvalue object is returned
Altogether: only three copy operators is called and no ctors at all!
Moreover I recommend you to return value by const reference, it won't allow you to write tricky and unobvious code. With cleaner code finding bugs will be much easier :) ( a = b ).f();
is better to split to two lines a=b; a.f();
.
P.S.:
Copy assignment operator : operator=(const Class& rhs)
.
Move assignment operator : operator=(Class&& rhs)
.
回答10:
If it returned a copy, it would require you to implement the copy constructor for almost all non-trivial objects.
Also it would cause problems if you declare the copy constructor private but leave the assignment operator public... you would get a compile error if you tried to use the assignment operator outside of the class or its instances.
Not to mention the more serious problems already mentioned. You don't want it to be a copy of the object, you really do want it to refer to the same object. Changes to one should be visible to both, and that doesn't work if you return a copy.