I do not understand copy constructors

Learner learner at gmail.com
Thu Aug 12 12:00:56 UTC 2021


On Thursday, 12 August 2021 at 11:07:24 UTC, drug wrote:
> 12.08.2021 12:36, Learner пишет:
>
>>  > It seems that there is no easy way to transition from a 
>> postblit to a
>> copy constructor, no?
>> 
>> 
>> 
>
> You just need both const and mutable copy ctors to replace 
> inout one:
> ```D
> struct A {
>     int[] data;
>     this(ref return scope A rhs) { data = rhs.data.dup; }
>     this(ref return scope const A rhs) const { data = 
> rhs.data.dup; }
> }
> ```
>
> the mutable copy ctor accepts mutable data and the const copy 
> ctor accepts const and immutable data

That still fails:

     Generating an `inout` copy constructor for `struct B` failed, 
therefore instances of it are uncopyable

Also if I remove the `const` body (can I assign data if the 
method is const?)

```D
struct A {
      int[] data;
      this(ref return scope A rhs) { data = rhs.data.dup; }
      this(ref return scope const A rhs) const {}
  }
Generating an `inout` copy constructor for `struct B` failed, 
therefore instances of it are uncopyable
```




More information about the Digitalmars-d-learn mailing list