Thread overview
Bypassing the const promise
Nov 19, 2009
Tomek Sowiñski
Nov 19, 2009
div0
Nov 20, 2009
Stewart Gordon
Nov 20, 2009
Tomek Sowiñski
November 19, 2009
const on a function forbids changing members:

class Wrong {
    int a;
    void foo() const {
        a = 4;
    }
}

The above rightly doesn't compile. But with a little twist...

class A {
    int a;
    void foo(ref int i) const {
        i = 4;
    }
    void foo() const {
        foo(a);
    }
}

void main() {
    auto a = new A;
    a.foo;
    assert(a.a == 4);
}

... I bypass the const promise on a function (two of them in fact). No casting, no evil stuff.

Is this a compiler bug or feature?

Tomek
November 19, 2009
Tomek Sowiñski wrote:
> const on a function forbids changing members:
> 
> class Wrong {
>     int a;
>     void foo() const {
>         a = 4;
>     }
> }
> 
> The above rightly doesn't compile. But with a little twist...
> 
> class A {
>     int a;
>     void foo(ref int i) const {
>         i = 4;
>     }
>     void foo() const {
>         foo(a);
>     }
> }
> 
> void main() {
>     auto a = new A;
>     a.foo;
>     assert(a.a == 4);
> }
> 
> ... I bypass the const promise on a function (two of them in fact). No casting, no evil stuff.
> 
> Is this a compiler bug or feature?
> 
> Tomek

That's definitely a bug.
Casting away const should always be explicit operation.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
November 20, 2009
Tomek Sowiñski wrote:
<snip>
> ... I bypass the const promise on a function (two of them in fact). No casting, no evil stuff.
> 
> Is this a compiler bug or feature?

http://d.puremagic.com/issues/show_bug.cgi?id=3534
November 20, 2009
Stewart Gordon Wrote:

> Tomek Sowiñski wrote:
> <snip>
> > ... I bypass the const promise on a function (two of them in fact). No casting, no evil stuff.
> > 
> > Is this a compiler bug or feature?
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=3534

Thanks!