Why can't we define re-assignable const reference variable?

Christopher Wright dhasenan at gmail.com
Sun Feb 17 06:22:48 PST 2008


none wrote:
> Actually we can rebind already: this is legal code accepted by the DMD compiler
> 
> for (i = n; i-- > 0; ) {
>   const B b = getConstB(i);  // are we rebind here n-times already?
>   b.doSomething();
> }

That doesn't really work. For purposes of const, it's as if you wrote:
void loopBody (int i) {
    const B b = getConstB(i);
    b.doSomething();
}

for (i = n; i > 0; i--) {
    loopBody(i);
}

> // the trouble is I cannot use the last 'b' i.e. getConstB(0),
> // anymore, it will be out of scope
> b.doSomethingElse();
> 
> 
>> (as we do now), I can always get around it by using D pointers:
>>   const(B)* b;            // type change
>>   b = &b1;                // rebind
>>   b = &b2;                // rebind
>> It's just so inconvenient, that's why I suggest allow define re-assignable const
>> reference variable.
> 

Then there's no way to say you don't want to be able to rebind it.



More information about the Digitalmars-d mailing list