Struct should be invalid after move

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Nov 28 00:28:35 UTC 2018


On Tuesday, November 27, 2018 12:51:12 PM MST Manu via Digitalmars-d wrote:
> The language goes to great effort to return everything to it's `init`
> state after being moved or destroyed. The whole point of that is to
> cover the cases you are concerned about here.
> If it was invalid to access a thing after it's moved (or after
> destruction), then there's no reason for any of the work that resets
> thing to their `init` to happen... that's quite a different set of
> language semantics.

I'd forgotten that move reset the variable to its init value. But given that
it does, that pretty much does eliminate this entire problem. I can
understand it if someone would still consider it a bug in their code to
reuse the variable given that it then doesn't hold the value that it did
before, but it's perfectly @safe to access it at that point, and memory
safety issues would have been the biggest reason to be concerned about
touching the variable again after it's been moved. As such, having the
compiler flag a variable being used after a move would be pretty much
equivalent to having it flag it if you used a variable that was simply
default-initialized rather than having been given an explicit value.

- Jonathan M Davis





More information about the Digitalmars-d mailing list