DIP 1018--The Copy Constructor--Community Review Round 1

Johannes Loher johannes.loher at fg4f.de
Sat Dec 22 08:24:15 UTC 2018


On Friday, 21 December 2018 at 14:43:50 UTC, Boris-Barboris wrote:
> On Tuesday, 18 December 2018 at 14:51:39 UTC, Mike Parker wrote:
> 1). I do not like the ability to specify a mutable copy source. 
> Under no circumstance should the code like
>
> A a;
> A fun()
> {
>     return a;      // lowered to return tmp.copyCtor(a)
> }
>
> void main()
> {
>     A b = fun();    // the return value of fun() is moved to 
> the location of b
> }
>
> be allowed to modify the value of a.

I totally agree. A copy modifying the source is very counter 
intuitive.

> 2). "A declaration is a copy constructor declaration if it is a 
> constructor declaration that takes only one non-default 
> parameter by reference that is of the same type as 
> typeof(this), followed by any number of default parameters..."
>
> If you need other parameters, you are not performing a copy. 
> Copy constructor needs no additional parameters. If the 
> semantics of your domain problem involve parametrized post-copy 
> operations, the code should state that explicitly - by using 
> specialized properly-named methods, that notify the reader 
> about this particularity.

I also agree with this. It is not even possible to pass something 
to the additional (default) parameters if not calling the Copy 
constructor explicitly, is it?

```
struct A {
     this(ref A src, int b = 0) {}
}

void main() {
     A a;
     auto b = a; // How to pass something as b?
     auto c = A(a, 1); // Works in this case
}
```

> 3). Section "Copy constructor usage", I don't understand the 
> difference:
>
> void main()
> {
>     A a;
>     A b = a; // copy constructor gets called
>     b = a;   // assignment, not initialization
> }
>
> and
>
> void main()
> {
>     A a;
>     a = fun();      // NRVO - no copy constructor call
>     A b; // b is initialized after this semicolon.
>     // why is this one not an assignment, but initialization?
>     // do we have the difference formally and consistently 
> defined for structs?
>     b = gun();      // NRVO cannot be performed, copy 
> constructor is called
> }

I believe the first case is straight forward, so your confusion 
probably stems from the second case:

In the main function, we do have assignments to a and b. Those 
are not initializations and so no copy construction is taking 
place because of this. But when functions return, if NRVO (named 
return value optimization) cannot be performed, a copy of their 
return value is created. This happens in the case of gun because 
a is a module level variable and so NRVO cannot be performed.

I believe this example is indeed a bit confusing, mostly because 
the DIP does not at all explain what actually happens here and 
only uses the cryptic acronym NRVO. I believe a more detailed 
description is needed here, maybe in conjunction with a 
comparison to how this example would work with postblit.




More information about the Digitalmars-d mailing list