RAII pointers
Moritz Maxeiner via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 3 13:13:30 PDT 2017
On Saturday, 3 June 2017 at 19:55:30 UTC, ag0aep6g wrote:
> On 06/03/2017 09:37 PM, Moritz Maxeiner wrote:
>> Of course, but AFAIK you'd need to explicitly assign it to an
>> object, so `ptr` won't null by accident, but only by explicit
>> programmer intent (same as overwriting the memory the object
>> lives in via things like `memcpy`); and you can always screw
>> things intentionally (you could also assign some invalid value
>> to the pointer via `memcpy`).
>
> I'd say `.init` can easily happen accidentally. Especially when
> `@disable this(this);` is involved.
>
> When you can't copy, you may have to move sometimes. But
> std.algorithm.move overwrites the old location with `.init`,
> assuming that `.init` can safely be destroyed.
Calling std.algorithm.move is explicit programmer intent, I
consider that about as accidental as calling memcpy with a source
full of zeroes.
In any case, having that check in the destructor is fairly cheap,
so better safe than sorry (and include it).
More information about the Digitalmars-d-learn
mailing list