Article View: comp.std.c++
Article #22Re: return type of operator=
From: Andrew Koenig
Date: Sat, 31 Oct 1992 14:10
Date: Sat, 31 Oct 1992 14:10
42 lines
1673 bytes
1673 bytes
In article <1992Oct31.025229.6520@cs.brown.edu> sdm@cs.brown.edu (Scott Meyers) writes:
> Is the return type of operator= an lvalue? In particular, if a, b, and c
> are of built-in type, is this legal?
> (a = b) = c;
Yes.
> My goal is to determine the correct type for user-defined operator=. If
> the return type is to be an lvalue, then for a class C, the return type
> should be C&.
It should be C&. The general rule is that any built-in operator that
returns one of its operands will do so as an lvalue if the operand is.
(except ?:, which yields an lvalue only if both the alternatives are
lvalues of the same type).
The thing that motivated this decision was a member function like this:
T& T::fun() {
// ...
return (*this = /*some value*/);
}
The person who wrote this thought that
return (*this = /*some value*/);
would mean the same as
*this = /*some value*/;
return *this;
but in a function that returns a T&, it means the same only if = yields a T&.
This is a strict extension from C: there is no way to write a strictly
conforming C program that is capable of detecting whether = returns an
lvalue.
--
--Andrew Koenig
ark@europa.att.com
Technical Details
Message-ID:
<24064@alice.att.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!arkReferences:
<1992Oct31.025229.6520@cs.brown.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark From: ark@alice.att.com (Andrew Koenig) Newsgroups: comp.std.c++ Subject: Re: return type of operator= Message-ID: <24064@alice.att.com> Date: 31 Oct 92 14:10:08 GMT Article-I.D.: alice.24064 References: <1992Oct31.025229.6520@cs.brown.edu> Reply-To: ark@alice.UUCP () Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 42