Struct should be invalid after move

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Nov 28 08:48:38 UTC 2018


On Wednesday, November 28, 2018 12:48:54 AM MST Sebastiaan Koppe via 
Digitalmars-d wrote:
> On Wednesday, 28 November 2018 at 00:28:35 UTC, Jonathan M Davis
>
> wrote:
> > 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.
>
> Yep. Exactly.
>
> I would love to opt-in such a feature via e.g.:
>
> struct Handle {
>     ...
>    @disable Handle.init;
>    ...
> }

If you have

@disable this();

in your struct, then that makes it illegal to default-initialize that type.
However, it still has an init value (since _all_ types in D have init
values), and so something like move would still use the init value, and any
type introspection using init would continue to work. It's just the default
initialization which is then illegal, forcing you to explicitly initialize
all variables of that type.

That being said, I wouldn't advise disabling default initialization unless
you really need to, because a number of things rely on it (e.g. I'm not sure
if you can stick a type which can't be default-initialized into an array,
because certain array operations depend on default initialization). So,
while it's occasionally useful, it's one of those things that tends to cause
problems when you do it.

- Jonathan M Davis





More information about the Digitalmars-d mailing list