Struct should be invalid after move

sanjayss dummy at dummy.dummy
Wed Nov 28 09:17:39 UTC 2018


On Tuesday, 27 November 2018 at 08:00:22 UTC, Sebastiaan Koppe 
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?

I have always wanted a feature in C that would let me explicitly 
tell the compiler that a variable is no longer in scope (some 
sort of unset of a variable). This would be useful to do 
defensive programming against use-after-free of pointers to 
allocated memory and such.

Though it would be nice to have the compiler auto-detect these 
kinds of things, maybe having the programmer explicitly request 
this might be way easier to implement.



More information about the Digitalmars-d mailing list