Struct should be invalid after move

Alex sascha.orlov at gmail.com
Tue Nov 27 12:42:43 UTC 2018


On Tuesday, 27 November 2018 at 12:03:20 UTC, Sebastiaan Koppe 
wrote:
> On Tuesday, 27 November 2018 at 10:59:03 UTC, Alex wrote:
>> There exist
>>
>> 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.

I mean, what would happen, if you take an existent function and 
pass Handle.init to it? Would it assert? Would it yield 42? Some 
reproducible behavior is defined well enough.

>> 2. Why do you don't want to treat the handle after movement 
>> the same way?
> Because the handle refers to an underlying resource, and any 
> access to that resource through that handle is invalid after a 
> move. Much like one doesn't want to call .release() twice on an 
> unique(T) wrapper type.
>
> 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.


More information about the Digitalmars-d mailing list