Struct should be invalid after move

Stanislav Blinov stanislav.blinov at gmail.com
Tue Nov 27 12:51:58 UTC 2018


On Tuesday, 27 November 2018 at 12:42:43 UTC, Alex wrote:
> On Tuesday, 27 November 2018 at 12:03:20 UTC, Sebastiaan Koppe 
> wrote:

>>> auto val = Handle.init;
>>>
>>> 1. How do you treat this?
>> I have no idea. Logically I would say that - in my case - the 
>> val is invalid.
>>
>
> This is a source of bugs. The T.init value is the one and only 
> value, which exists, independently from the definition (of 
> Handle). Therefore, your own functions on Handle have to 
> consider this case and have to behave properly. I.e. define 
> some behavior.

No. The only thing that must be valid to do with a value in .init 
state is destruct it. Standard library move et al. assume as 
much. In fact, what they do is exactly that - wipe out the moved 
from value, emplacing the initializer there. What also *may* be 
valid is reinitialization/assignment.

>> Sure, I could put in a runtime check, but then I get runtime 
>> errors and I rather have compile time errors.
>
> This is not an answer to my question. The question was: why you 
> would like to have a different behavior w.r.t. some other 
> behavior, which exists, independent of your actions.

Because .init does not generally exert any useful behavior.


More information about the Digitalmars-d mailing list