If a type has invariants that can be broken by moving out a member, it should have user-defined constructors/operators to maintain those invariants, std::indirect or not.
And even if a lone member having unexpected value may break the normal program operation, it is fundamentally different from a potential UB.
Note that moved out state still has to be valid, not just unspecified. It won't be some random bytes but a value that object of that type could have had anyway.
It's not only types that have invariants. Whenever you write a loop or similar multi-step algorithm, you are thinking in terms of invariants. And not all invariants will be protected behind a class, nor would it be reasonable in general to do so.
There's nothing you can really do to protect your program against injection of random garbage data at uncontrolled points due to a moved-from object being accessed.
View all comments
Show parent comment
1
u/cfyzium Aug 14 '26
If a type has invariants that can be broken by moving out a member, it should have user-defined constructors/operators to maintain those invariants, std::indirect or not.
And even if a lone member having unexpected value may break the normal program operation, it is fundamentally different from a potential UB.
Note that moved out state still has to be valid, not just unspecified. It won't be some random bytes but a value that object of that type could have had anyway.