I do not understand copy constructors

Steven Schveighoffer schveiguy at gmail.com
Fri Aug 13 15:26:15 UTC 2021


On 8/12/21 12:12 PM, Paul Backus wrote:
> The reason for this is a bit subtle. Normally, `inout` can convert to 
> `const`, so you might expect that the `const` copy constructor could be 
> used to construct a copy of an `inout` object. However, copy 
> constructors take their arguments by `ref`, and implicit conversions are 
> not allowed for arguments passed to `ref` parameters. (I cannot find a 
> citation in the spec for this, but you can verify it yourself.)

implicit const conversions are possible via a single ref, but not 
through a double reference. In this case, it's not the passing of the 
inout object to the const constructor.

The issue is that you can't convert const (or immutable or mutable) to 
inout implicitly, and the member variable is inout inside an inout 
constructor. Therefore, there's no viable copy constructor to call for 
the member, and the outer copy constructor cannot be generated.

As a side note, inout actually *can* bind to double references of any 
mutability, unlike const, which is a nice feature that is seldom talked 
about.

-Steve


More information about the Digitalmars-d-learn mailing list