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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 11 02:28:26 UTC 2020


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

--- Comment #9 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Илья Ярошенко from comment #4)
> btw, I don't see whare spec says that an aggregate can't hold both old and
> new style members
> 
> struct C
> {
>     SOld sOld;
>     SNew sNew;
> }
> 
> void main()
> {
>     C c;
>     auto d = c;
>     assert(d.sOld.s);
>     assert(d.sNew.s);
> }
> 
> This is a buggy definition too and doesn't pass second assert too.
> 
> The spec doesn't say that this shouldn't work.
> It just ignores this case. But this is definitely the language design bug.

It does not say that explicitly, however, the DIP specifies that if you have a
postblit (user-defined or generated) then copy constructors will be ignored. In
your case, one of the fields has a postblit and therefore struct C will have a
postblit generated for it as per the rules in [1]. Now the rules stated in the
copy constructor DIP enter the scene and therefore a copy constructor is not
going to get generated.

It is not a bug, it a language design choice to favor backwards compatibility
so that codebases that heavily rely on the postblit will not be affected by the
insertion of new structs that define copy constructors.

[1] https://dlang.org/spec/struct.html#struct-postblit

--


More information about the Digitalmars-d-bugs mailing list