Struct should be invalid after move

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Nov 27 09:39:13 UTC 2018


On Tuesday, November 27, 2018 1:00:22 AM MST Sebastiaan Koppe via 
Digitalmars-d wrote:
> I have a non-copyable struct and I really want a compiler error
> whenever I access it after it has been moved.
>
> ---
> struct Handle {
>      ...
>      @disable this(this);
>      ...
> }
>
> void main() {
>      import std.algorithm : move;
>      auto handle = getOne();
>      auto second = handle.move;  /// line 14
>      auto third = handle.move;    ///  <- compiler error, variable
> handle is invalid after line 14
> }
> ---
>
> I believe this would prevent some nasty bugs when dealing with
> these structs.
>
> What do you think?

Well, the DIP to add opPostMove has been approved, and if opPostMove is then
@disabled, then that should disable moving entirely (making it a compile
error for it to happen at all, not to access the struct after it's been
moved), which is probably a better approach. However, that will require that
the DIP actually be implemented, and AFAIK, there's no ETA on that.

- Jonathan M Davis





More information about the Digitalmars-d mailing list