How are extra copy constructor parameters used?

Paul Backus snarwin at gmail.com
Thu Dec 30 01:14:16 UTC 2021


On Thursday, 30 December 2021 at 01:04:10 UTC, Ali Çehreli wrote:
> 2) Unlike the examples there, I think the parameter should most 
> usefully be defined as 'const' unless there is a special reason:
>
> struct S {
>
>   // const(S) instead of S:
>   this(ref const(S) that) {
>   }
> }
>
> Do you agree?

When the compiler generates a copy constructor for you, it always 
qualifies both the source and destination objects with `inout`:

https://dlang.org/spec/struct.html#implicit-copy-constructors

Therefore, when you write your own copy constructors, you should 
always use `inout` if possible, so that compiler-generated copy 
constructors will be able to copy instances of your struct that 
appear as members of other structs.


More information about the Digitalmars-d-learn mailing list