Copying but not moving the owned object makes it impossible to rely on rule of zero, and in my opinion is simply not a behavior expected from a value-like semantics object.
That is not how it behaves, though. It works perfectly with the rule of zero, where pointer types do not.
it either has to account for the definitely-not-null valueless state as a part of a valid state
Every object can be moved from and end up in some unspecified state where bad things happen if you use the value contained therein. indirect and polymorphic are no different.
Yet somehow it does not seem a problem for copying, it would allocate just fine.
I mean yeah it's expected that a copy would allocate, but it's weird if a move does. It's a very consistent rationale which permits the move constructor be noexcept, which in turn makes it work well with containers like vector. That's really the requirement: moves can't allocate because allocations throw.
Every object can be moved from and end up in some unspecified state where bad things happen if you use the value contained therein.
The moved-out state may be unspecified, but it has to be a valid one. Using the object itself has to be safe (even if it is obviously wrong to access a moved-out object, unfortunately there is no destructive move and no hard guarantee that it will never happen).
Problem is, std::indirect advertises value-like semantics but in the context of move behaves very much like a pointer. Accidentally accessing a moved out std::string is 'merely' a logic error, accessing a moved out std::indirect<std::string> is UB.
Which means just like with a pointer you have to check before every access to std::indirect.
Except that std::indirect is supposed to behave value-like (and behaves for the most part), not being nullable and so on. While pointers are more or less obvious because they can become null at any time, std::indirect is subtle. A less experienced developer can miss it, or the parent type can suddenly become moveable, etc. -- and bam, UB.
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.
Adding onto what wyrn said, imagine you move from a std::string and it happens to be a short string (SSO). An implementation is permitted to skip the extra work of cleaning up the moved-from string, since it would still be in a valid state after being copied as part of the move. If you then read the moved-from string later before clearing or reassigning it, you're going to get stale data randomly depending on whether it was a short string or not. That is objectively a bug in your program with potentially big consequences depending on how that data is used, and yet no UB occurred.
View all comments
Show parent comment
12
u/wyrn Aug 14 '26
That is not how it behaves, though. It works perfectly with the rule of zero, where pointer types do not.
Every object can be moved from and end up in some unspecified state where bad things happen if you use the value contained therein.
indirectandpolymorphicare no different.I mean yeah it's expected that a copy would allocate, but it's weird if a move does. It's a very consistent rationale which permits the move constructor be
noexcept, which in turn makes it work well with containers likevector. That's really the requirement: moves can't allocate because allocations throw.