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

Paul Backus snarwin at gmail.com
Sat Jan 5 21:34:30 UTC 2019


On Saturday, 5 January 2019 at 13:34:09 UTC, RazvanN wrote:
> On Friday, 21 December 2018 at 14:43:50 UTC, Boris-Barboris 
> wrote:
>> 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.
>>
>>
> Agree, that was my initial design too, but in C++ you can 
> define any number of
> default parameters and people coming from that background might 
> have some cases
> where it is useful for them to have default parameters. Since 
> this is the first
> iteration of a copy constructor implementation, we should try 
> to stick as much
> as possible to the C++ design. Of course, in the case of const 
> source we cannot
> do that since const means something else in D.

This seems like a weak justification to me. There are many things 
in C++ that are permitted by the language but considered poor 
practice by the C++ community, and continue to exist only for the 
sake of backward compatibility. D should not blindly adopt 
behavior from C++ without considering why that behavior exists in 
C++, and whether D could do better.

I have not been able to find many sources on whether copy 
constructors with optional parameters are regarded as useful or 
good practice in the C++ community, but the results I have found 
are not encouraging. In the C++ Core Guidelines, the section on 
copy constructors [1] does not even acknowledge the possibility 
of additional optional parameters. In addition, at least one 
static analysis tool for C++ considers copy constructors with 
optional parameters an error [2], because they may be called 
implicitly in places the programmer did not intend them to be. 
None of this is conclusive evidence, of course, but at the very 
least, it indicates that this is an issue on which the C++ 
community is divided.

If there are specific, known benefits to allowing copy 
constructors with optional parameters in D, then the DIP should 
articulate those benefits. "C++ does it that way" is not good 
enough. Otherwise, it may be better to go ahead with only 
single-parameter copy constructors, and leave the question of 
copy constructors with optional parameters for a future DIP.

[1] 
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-ctor
[2] 
https://help.semmle.com/wiki/display/CCPPOBJ/Constructor+with+default+arguments+will+be+used+as+a+copy+constructor


More information about the Digitalmars-d mailing list