Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing value() for void types #110

Closed
wants to merge 3 commits into from

Conversation

conr2d
Copy link

@conr2d conr2d commented Apr 12, 2022

Close #108

@vasama
Copy link

vasama commented Jul 28, 2022

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

@vasama
Copy link

vasama commented Jul 28, 2022

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

@conr2d
Copy link
Author

conr2d commented Jul 28, 2022

@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

@vasama
Copy link

vasama commented Jul 28, 2022

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.

@conr2d
Copy link
Author

conr2d commented Jul 28, 2022

@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.

@vasama
Copy link

vasama commented Jul 28, 2022

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.

@conr2d
Copy link
Author

conr2d commented Jul 28, 2022

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

@conr2d conr2d closed this Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expected<void, E>::value()
2 participants