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

Boris-Barboris ismailsiege at gmail.com
Fri Dec 21 14:43:50 UTC 2018

On Tuesday, 18 December 2018 at 14:51:39 UTC, Mike Parker wrote:
> This is the feedback thread for the first round of Community 
> Review for DIP 1018, "The Copy Constructor"

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. This is an absolute pain to 
read\debug, and I would instead like to see a mandatory 
const\immutable on the source reference.
Copy operation should not modify the source, or it should not be 
called a copy. If we are talking about a typical smart pointer 
struct (Refcounted), copy still should not modify the source. It 
is D's transitive const\immutable that is a problem here and it 
must be explicitly casted away by the developer.

Only const also mitigates the combinatorial mess caused by 4 
combinations of mutable\immutable copy constructors, since 
immutable is implicitly converted to const.

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 

3). Section "Copy constructor usage", I don't understand the 

void main()
     A a;
     A b = a; // copy constructor gets called
     b = a;   // assignment, not initialization


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

More information about the Digitalmars-d mailing list