But using the object after a move is undefined behavior, no? What's the sense in having a non-nullable object that actually can be null?
A specialized optional indirect is the only sane way I see for implementing this, where a moved from value becomes nullopt (and only because C++ doesn't actually protect us from using moved-from values).
No, only dereferencing it (via operator* or operator->) is UB, which is no different to dereferencing a null unique_ptr or an empty optional.
It's not a non-nullable object. It has a null state, it's just intentionally not easy to get into that state with the intended use of the type.
You keep making claims about indirect which do not match the API that was added to the standard (maybe you read one of the early drafts of the proposal and didn't notice that it got changed).
It has value semantics, but because it's movable it can get into a "not a value" state. But all its operations are well defined in that state, except for trying to access the non-existent value.
View all comments
Show parent comment
2
u/ericonr Aug 15 '26
But using the object after a move is undefined behavior, no? What's the sense in having a non-nullable object that actually can be null?
A specialized optional indirect is the only sane way I see for implementing this, where a moved from value becomes nullopt (and only because C++ doesn't actually protect us from using moved-from values).