[Issue 21349] copy and postblit constructors aren't compatible

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 11 02:16:54 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=21349

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #7 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Илья Ярошенко from comment #6)
> I have thought think about these lines
> 
> > For backward compatibility reasons, a struct that defines both a copy
> > constructor and a postblit will only use the postblit for implicit copying.
> 
> What they are really about?
> 
> First, they say about "struct that defines both ", but in the second case
> the struct doesn't define neither. The compiler generates something, and we
> can suppose that compiler can generate something good. For example a new
> style copy constructor. And it should generate it instead of postblit. Why?
> because the second:
> 
> Second, "For backward compatibility reasons". The reason is backward
> compatibility, not something else.
> 
> I mean that "struct that defines both" should be understood as "struct that
> _explicilty_ defines both".
> 
> So, even with the current spec, this can work, and if it can work, then it
> should work.

I agree that the spec might be misleading, however, the DIP has a more exact
formulation [1]:

"In order to assure a smooth transition from postblit to copy constructor, the
following strategy is employed: if a `struct` defines a postblit (user-defined
or generated) all copy constructor definitions will be ignored for that
particular `struct` and the postblit will be preferred. Existing code bases
that do not use the postblit may start using the copy constructor without any
problems, while codebases that rely on the postblit may
start writing new code using the copy constructor and remove the deprecated
postblit from their code."

So from the beginning you must either use one or other; having both in your
code will not fly. In this specific example, you have 2 solutions: either you
replace the postblit with a copy constructor everywhere (which is the preferred
solution) or you simply switch C to a copy constructor and later on update the
code base to get rid of postblits.


[1]
https://github.com/dlang/DIPs/pull/129/files#diff-ecee0474c4314cd47dd8c2656b485c0cfd56e704a85de75839ec2850fb61f0ebR282

--


More information about the Digitalmars-d-bugs mailing list