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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Oct 29 16:59:20 UTC 2020


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

Paul Backus <snarwin+bugzilla at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |snarwin+bugzilla at gmail.com
           Severity|blocker                     |enhancement

--- Comment #2 from Paul Backus <snarwin+bugzilla at gmail.com> ---
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.

--


More information about the Digitalmars-d-bugs mailing list