this as lvalue?

Jonathan M Davis jmdavisprog at gmail.com
Fri Sep 3 14:33:46 PDT 2010


On Friday 03 September 2010 14:22:46 Andrei Alexandrescu wrote:
> On 9/3/10 16:03 CDT, bearophile wrote:
> > JMRyan:
> >> Is this a bug in the compiler (v.2.047)?  Am I missing something in
> >> thinking it shouldn't be?
> > 
> > I think it's not a bug. It's not a common need, but a method may way want
> > to swap this with another. In Phobos this is done on a struct, see:
> > http://www.dsource.org/projects/phobos/browser/trunk/phobos/std/stdio.d#
> > L324
> > 
> > Bye,
> > bearophile
> 
> For classes this must be an rvalue.
> 
> Andrei

There is no value in this being assignable in a class, but at the lower level of 
how the functions are actually declared underneath (with them taking this as an 
argument), it makes perfect sense that it would work. It certainly wouldn't be 
bad to disallow it though, since it's pointless and is just going to cause bugs, 
though generally not as bad as the ones in the OP's example (since he assigned 
to this in the constructor).

Still, I don't know how you could make it a true rvalue. You'd need the ability 
to pass a const reference to non-const data to do that, and D doesn't allow for 
that. The invisible this parameter, being a reference, is going to suffer from 
all of the issues with const that have been oft-discussed (though, generally 
what people want is a non-const reference to const data rather than a const 
reference to non-const data like you would need for this).

- Jonathan M Davis


More information about the Digitalmars-d mailing list