About foreach loops

Timon Gehr timon.gehr at gmx.ch
Fri Jun 17 08:25:46 PDT 2011


Jesse Phillips wrote:
> On Wed, 15 Jun 2011 16:40:24 -0400, bearophile wrote:
>
>> Isn't it undefined in D to modify a const?
>
> Yes, but unlike what every redditer is say, that doesn't mean the
> compiler can do whatever it wants. It means you can't define its
> behavior. Go ahead try.
> [snip.]

I tried:
void main(){
    const c=128;
    auto p = &c;
    *cast(int*)p = 64;
    assert(p==&c);
    assert(*p!=c);
    //dmd 2.053: PASS
}

void main(){
    for(const i=0;i<32;++*cast(int*)&i){}
    assert(0); // 'pass' ;)
}

Another compiler might decide to fail both assertions or do some funny stuff.

Timon


More information about the Digitalmars-d mailing list