Error: cannot implicitly convert expression (this) of type const(S) to S

Jonathan M Davis jmdavisProg at gmx.com
Sat Sep 18 02:15:38 PDT 2010


Okay, if I try and compile the following program.

struct S
{
    @property S save() const
    {
        return this;
    }

    int[] _val;
}

void main()
{
}


I get the error message

d.d(5): Error: cannot implicitly convert expression (this) of type const(S) to S


If I remove const from save(), then it works, but with const there, it doesn't. 
If I change _val to int or a struct type which does not contain an array, it 
works. If I change _val to a struct type which has a member variable which is an 
array, it doesn't work.

From the looks of it, there's something about having an array as a member 
variable which makes this blow up. Perhaps it has to do with the fact that _val 
is a reference type and would be shared? Do I need to declare a postplit 
constructor to fix this? Declaring one doesn't seem to help, but maybe I just 
don't know how to declare them correctly. I tried

this(this)
{
    _val = _val.dup;
}


and it didn't do any good. Setting _val to null didn't work either.

Does anyone have any clues as to what I'm doing wrong? Or is this yet another 
bug in dmd?

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list