In practice, accessing a "valid but unspecified value" will corrupt the state of your program, which, while not technically delete-the-hard-drive-and-fire-the-missiles bad, is still likely to corrupt the state of your program, break your invariants, and lead to unpredictable results. It's not formally UB according to the standard, but it behaves much like it in the context of your program as your own guarantees fail to hold.
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
4
u/wyrn Aug 14 '26
In practice, accessing a "valid but unspecified value" will corrupt the state of your program, which, while not technically delete-the-hard-drive-and-fire-the-missiles bad, is still likely to corrupt the state of your program, break your invariants, and lead to unpredictable results. It's not formally UB according to the standard, but it behaves much like it in the context of your program as your own guarantees fail to hold.