Ouch: return values as lvalue

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Tue Jan 30 09:12:43 PST 2007


Lionello Lunesu wrote:
> Consider this home-made const:
> 
> struct Task {
>   char[] ID;
> }
> private Task _CurrentTask; //mutable
> public Task CurrentTask() { return _CurrentTask; } //const
> 
> public void StopTask() {
>   CurrentTask.ID = null;
> }
> 
> Notice the bug? That last line should read "_CurrentTask.ID = null;"
> 
> Isn't there something the compiler can do to help me catch these bugs?

const will take care of it. The code above fetches a member of an 
rvalue, which is an lvalue.

Andrei



More information about the Digitalmars-d mailing list