On Wednesday, 17 February 2021 at 20:18:53 UTC, Paul Backus wrote:
On Wednesday, 17 February 2021 at 19:42:00 UTC, tsbockman wrote:
A copy constructor and opAssign can be used to update pointers that are relative to &this:
    https://dlang.org/spec/struct.html#struct-copy-constructor

Unfortunately this is not enough, because the compiler is free to implicitly move struct instances in memory any time it wants to. See the bug report below for more details:

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

Until D gets move constructors, structs with interior pointers should be avoided.

That bug is about postblits, this(this), not copy constructors: this(ref typeof(this)). Copy constructors were added to the language specifically to fix those sort of problems. From the spec:

https://dlang.org/spec/struct.html#struct-postblit
WARNING: The postblit is considered legacy and is not recommended for new code. Code should use copy constructors defined in the previous section. For backward compatibility reasons, a struct that explicitly defines both a copy constructor and a postblit will only use the postblit for implicit copying. However, if the postblit is disabled, the copy constructor will be used. If a struct defines a copy constructor (user-defined or generated) and has fields that define postblits, a deprecation will be issued, informing that the postblit will have priority over the copy constructor.

However, since issue #17448 is still open I have posted a question to the issue report asking for clarification:
    https://issues.dlang.org/show_bug.cgi?id=17448#c39

Reply via email to