Struct should be invalid after move

Sebastiaan Koppe mail at skoppe.eu
Tue Nov 27 10:53:21 UTC 2018


On Tuesday, 27 November 2018 at 09:50:25 UTC, John Colvin wrote:
> void foo(int a)
> {
>     import std.algorithm : move;
>     auto handle = getOne();
>     if (a > 0)
>         auto second = handle.move;
>     auto third = handle.move; // compile error?
> }
>
> Not a trivial problem in the general case.

Not a trivial problem indeed. But I really believe we need these 
things (or similar) for safety's sake.

One other issue I can see is if the handle is passed by ref into 
opaque function. At this point we can't reason about whether the 
struct is still valid after the call to bar:

---
void bar(ref Handle);

void main() {
   auto handle = getOne();
   bar(handle);
   auto second = handle.move(); // compile error? don't know
}
---

Possible solution is to always disallow move after passing as ref.

And maybe move could be disallowed on a scope ref param, then we 
could safely pass Handle to functions that take by ref while 
provably keeping the caller's Handle valid.


More information about the Digitalmars-d mailing list