expected icon indicating copy to clipboard operation
expected copied to clipboard

Add missing value() for void types

Open conr2d opened this issue 2 years ago • 7 comments

Close #108

conr2d avatar Apr 12 '22 10:04 conr2d

value() must throw when the expected has no value.

vasama avatar Jul 28 '22 06:07 vasama

The four overloads for different value categories are redundant. A single const overload is sufficient.

vasama avatar Jul 28 '22 06:07 vasama

@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

conr2d avatar Jul 28 '22 10:07 conr2d

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 avatar Jul 28 '22 10:07 vasama

@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() is false. 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.

conr2d avatar Jul 28 '22 11:07 conr2d

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 avatar Jul 28 '22 11:07 vasama

@vasama Ah, you are right. I forgot considering that case.

conr2d avatar Jul 28 '22 11:07 conr2d