Struct copy constructor with inout

dhs dhs at email.com
Tue Nov 14 08:50:34 UTC 2023


Hello D experts,

I have a question regarding inout in struct copy constructors.
 From the spec:

"The copy constructor can be overloaded with different qualifiers 
applied to the parameter (copying from a qualified source) or to 
the copy constructor itself (copying to a qualified destination)"

I am using following code:

```d
struct S1
{
     this(ref const S1 s) const { writeln("copy"); }
     int i;
}

struct S2
{
     this(ref inout S2 s) inout { writeln("copy"); }
     int i;
}

void test()
{
     const(S1) s1;
     S1 ss1 = s1; // error, ss1 not qualified as const

     const(S2) s2;
     S2 ss2 = s2; // fine, why?
}
```

Isn't "inout" supposed to copy the const-ness of its parameter to 
the constructor's attribute? In other words: why doesn't ss2=s2 
fail here?

Thanks,
dhs


More information about the Digitalmars-d-learn mailing list