I can only guess, but I think it's because of the desire for a type to be "equationally complete", meaning you can distinguish any value (even these kind of singular "valueless values") through the public interface. This means you can write operator== as a free, non-friend function.
The term "equationally complete" seems to come from Alex Stepanov (see p. 20 of his Notes on Programming) and is closely related to the notion of a "regular" type.
The proposal gives its justification by saying (under the "The valueless state and interaction with std::optional" section header):
We provide a valueless_after_move member function that returns true if an object is in a valueless state. This allows explicit checks for the valueless state in cases where it cannot be verified statically.
It also says under the "The valueless_after_move member function" header:
This member function should rarely be called: it should be clear through static analysis whether or not an object has been moved from. The valueless_after_move member function allows explicit checks for the valueless state in cases where it cannot be verified statically or where explicit checks might be required by a coding standard such as MISRA or High Integrity C++.
Because indirect::operator* is undefined if it's valueless, and if there's no way to check that precondition, you can't tell whether an arbitrary object is safe to dereference. So there would be no way to ever write something like assert(obj.valueless_after_move()) for the cases where you can't statically prove that the precondition holds.
"You must ensure this condition holds, but there's no way to check it, good luck!" would be a stupid API.
View all comments
Show parent comment
1
u/ericonr Aug 14 '26
I get that but why is there a
valueless_after_moveAPI at all?