Return const object through mutable Object
Jonathan M Davis
jmdavisProg at gmx.com
Sun Apr 15 17:33:43 PDT 2012
On Sunday, April 15, 2012 17:57:27 Jacob Carlborg wrote:
> class Foo
> {
> Foo f;
>
> Foo bar () const
> {
> return f;
> }
> }
>
> The above code results in the compile error:
>
> Error: cannot implicitly convert expression (this.f) of type const(Foo)
> to test.Foo
>
> But if I change "bar" to:
>
> Object bar () const
> {
> return f;
> }
>
> I don't get any errors. Is the last example supposed to compile?
>
> I'm using DMD v2.059.
It definitely looks like a bug. Whether it returns Foo or Object should have no
effect on constness, and since f is a member variable of Foo, and the this
pointer/reference is const, bar is going to have to return a const reference
to it, which the second version isn't doing.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list