expected
expected copied to clipboard
Add missing value() for void types
Close #108
value()
must throw when the expected
has no value.
The four overloads for different value categories are redundant. A single const
overload is sufficient.
@vasama
I think so, but P0323R11 defines four overloads for std::expected<T, E>::value()
:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0323r11.html#expected.expected
According to P0323R11, it's not clear how std::expected<void, E>::value()
should work, but original author's implementation also doesn't throw an exception:
https://github.com/viboes/std-make/blob/master/include/experimental/fundamental/v3/expected2/expected.hpp#L552-L563
On further inspection, I was wrong about the overloads being redundant. The &&
overload moves the error object when throwing it.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0323r11.html#expected.void.observe
Clearly states that an exception is thrown when has_value()
is false
. The implementation you reference is also faulty.
@vasama
On further inspection, I was wrong about the overloads being redundant. The
&&
overload moves the error object when throwing it.
Thank you for letting me know.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0323r11.html#expected.void.observe Clearly states that an exception is thrown when
has_value()
isfalse
. The implementation you reference is also faulty.
std::expected<void, E>
can be constructible where std::expected<void, E>::has_value() == true
.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0323r11.html#expected.void.ctor
constexpr expected() noexcept;
Postconditions: has_value() is true.
To be clear, the &
and const&&
overloads are still redundant as they both copy the error object.
Yes, an expected<void, E>
where has_value() == true
can of course be constructed, but so can one where has_value() == false
. value()
throws if and only if has_value() == false
.
@vasama Ah, you are right. I forgot considering that case.