Subject: Re: standards/5959: c++ language specification error or compiler
To: der Mouse <mouse@Rodents.Montreal.QC.CA>
From: Andrew Brown <twofsonet@graffiti.com>
List: netbsd-bugs
Date: 08/13/1998 08:19:27
>> Parameters are evaluated before calling the function. This is true
>> also for parameters with side effects.
>
>Yes...but the parameter here *isn't an lvalue*! Yes, it's evaluated
>before the call, and there's a sequence point just before the call.
>But why does the reference refer to foo? What's being passed isn't
>foo, but rather the value that just got assigned to foo.
well...not the value that just got assigned to foo, but rather, the
result of the assignment. consider assigning a long double to a short
int. the result is most certainly a short int. :)
i have a class where the result of an assignment (from a char* or an
int) is an int (either a 0 or a 1) that indicates whether or not the
assignment worked. some instances of the class are "read-only".
...so it should either (a) pass a reference to an anonymous object, or
(b) generate a compiler error (since the compiler knows it would have
to generate an anonymous object), right?
>Or are assignments lvalues in C++? I would certainly hope not.
in c you can certainly *not* do
(foo=3)=4;
which is effectively what's going on here. although as i mentioned,
if you define a class that returns a non-const reference from the
operator=(), ie:
class bar {
public:
bar& operator=(const bar &t) {
... /* do "assignment" */
return *this;
}
};
you can make an assignment return an lvalue. the more "proper" way to
do this would be to make the ref that's returned be a const ref (as in
the parameter to operator=()) so that you specifically *can't* pass it
to another function that expects a non-const reference.
it's probably not "proper" to do it with assignable assignments, but
you can make it work. you might even want to.
--
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org * "ah! i see you have the internet
twofsonet@graffiti.com (Andrew Brown) that goes *ping*!"
warfare@graffiti.com * "information is power -- share the wealth."