Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 19, 2009 Bypassing the const promise | ||||
---|---|---|---|---|
| ||||
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 Re: Bypassing the const promise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiñski Attachments: | 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 Re: Bypassing the const promise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiñski | 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 Re: Bypassing the const promise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | 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!
|
Copyright © 1999-2021 by the D Language Foundation