On Thursday, 12 August 2021 at 11:32:03 UTC, Paul Backus wrote:
On Thursday, 12 August 2021 at 11:19:34 UTC, drug wrote:
```D
struct A {
    int[] data;
this(ref return scope inout A rhs) /* no inout here */ { data = rhs.data.dup; }
}
```
The problem is that if you qualify the ctor itself then if you pass const/immutable rhs to it then the ctor is const/immutable too (like the args) and of course you cannot modify this, so the error.

To make a copy ctor you need to qualify copy ctor args as inout but the copy ctor itself shall be mutable and have no const,immutable or inout qualifier.

This is not true. Qualifying the ctor as `inout` works fine: https://run.dlang.io/is/Kpzp5M

The problem in this example is that `.dup` always returns a mutable array, even if the array being copied is `inout`. The solution is to cast the copy back to the original type:

```d
this(ref return scope inout A rhs) inout
{
    data = cast(typeof(rhs.data)) rhs.data.dup;
}
```

That worked fine, but the codebase is @safe:

```d
cast from `int[]` to `inout(int[])` not allowed in safe code
```

So copy constructors force me to introduce trusted methods, while that was not necessary with postblits?


Reply via email to