c++ - Would it be a good practise to store a 'validness' state for movable objects? -


i designing library, many of classes movable. many of movable classes passed arguments functions of other classes. thinking how minimize code validation checks. instances of movable classes in valid state after constructing, become invalid after being moved from.

would practise have flag 'valid' true after constructing , becomes false after moving. way object valid again when valid object moved it.

i'll mention after moving objects not go in state calling functions on them cause undefined behavior or anything. it's after moving contents garbage.

should i, or shouldn't i?

such flag might suitable debugging purposes, it's developer using library/code make sure he/she never uses objects in way quirky after have been used from.

the whole purpose of move-constructors , move-assignments move data src dst, makes src contain nothing garbage, , developer using such functionality should aware of this.

note: constructs should never ill-formed includes assignment operator(s), 1 should able assign new data moved-from object.


since move object accessed via variable (ie. lvalue) happens if developer explicitly says so 1) developer has such code signed invisible contract renders him/her responsible side-effects of using object after being moved from.

note: 1) via std::move (val), static_cast<t&&> (val), or equivalent.



the standard library

if in standard library see there no valid flag in practice, instead (as said) developer make sure doesn't use invalid constructs on moved-from object.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -