Ouch: return values as lvalue
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Tue Jan 30 11:24:56 PST 2007
Lionello Lunesu wrote:
> "Andrei Alexandrescu (See Website For Email)"
> <SeeWebsiteForEmail at erdani.org> wrote in message
> news:45BF7C8B.3080506 at erdani.org...
>> 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.
>
> Yeah, I know, but it's odd: it's setting a member in a struct that's about
> to be deleted. It's like writing "{ int id=4; }". Wouldn't it be possible
> for the compiler to warn about "code without side-effect" or something?
Probably it could, but sometimes the object will be used:
foo(bar().baz += 1);
But that can be detected, too. But in generic code you can't rely on one
specific expectation. And Walter hates warnings :o).
Andrei
More information about the Digitalmars-d
mailing list