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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 11 02:32:57 UTC 2020


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

--- Comment #10 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Paul Backus from comment #2)
> This is an enhancement request, not a bug. Per the language spec:
> 
> > For backward compatibility reasons, a struct that defines both a copy
> > constructor and a postblit will only use the postblit for implicit copying.
> 
> Source: https://dlang.org/spec/struct.html#struct-postblit
> 
> You can work around this by placing sOld in a union, since unions do not
> have generated postblits:
> 
> struct C
> {
>     union { SOld sOld; }
>     SNew sNew;
> 
>     this(ref C other) {
>         pragma(inline, false);
>         sOld = other.sOld;
>         sNew = other.sNew;
>     }
> }
> 
> Of course, if SOld has any other special member functions, such as a
> destructor or identity opAssign overload, you will have to implement those
> manually for C as well.

A better solution would be to simply disable the postblit in C and everything
will work as you expect it.

--


More information about the Digitalmars-d-bugs mailing list