-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Implement P2505R5 Monadic Functions For std::expected #3361
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question from me (regarding all occurrences, not just this one)
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
At least we could extract the common parts into a helper function template. Currently the definition of the 42-line implementation that I was thinking of template <class _SelfType, class _Fn>
static constexpr auto _Transform_expected(_SelfType&& _Self, _Fn&& _Func) {
_STL_INTERNAL_STATIC_ASSERT(is_same_v<remove_cvref_t<_SelfType>, expected>);
using _Uty = remove_cv_t<invoke_result_t<_Fn, decltype(*_STD declval<_SelfType>())>>;
if (_Self._Has_value) {
if constexpr (is_void_v<_Uty>) {
_STD invoke(_STD forward<_Fn>(_Func), _STD forward_like<_SelfType>(_Self._Value));
return expected<_Uty, _Err>();
} else {
return expected<_Uty, _Err>(_Construct_expected_from_invoke_result_tag{},
_STD forward<_Fn>(_Func), _STD forward_like<_SelfType>(_Self._Value));
}
} else {
return expected<_Uty, _Err>(unexpect, _STD forward_like<_SelfType>(_Self._Unexpected));
}
}
template <class _Fn>
requires is_copy_constructible_v<_Err>
constexpr auto transform(_Fn&& _Func) & {
return _Transform_expected(*this, _STD forward<_Fn>(_Func));
}
template <class _Fn>
requires is_copy_constructible_v<_Err>
constexpr auto transform(_Fn&& _Func) const& {
return _Transform_expected(*this, _STD forward<_Fn>(_Func));
}
template <class _Fn>
requires is_move_constructible_v<_Err>
constexpr auto transform(_Fn&& _Func) && {
return _Transform_expected(_STD move(*this), _STD forward<_Fn>(_Func));
}
template <class _Fn>
requires is_move_constructible_v<_Err>
constexpr auto transform(_Fn&& _Func) const&& {
return _Transform_expected(_STD move(*this), _STD forward<_Fn>(_Func));
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some nitpicking
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Note to self: I reviewed the product code up to the void
specialization and still need to review the new test file.
All of these _Uty types are specializations of expected.
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
tests/std/tests/P2505R5_monadic_functions_for_std_expected/test.cpp
Outdated
Show resolved
Hide resolved
Thanks! I finished reviewing and pushed changes for the remaining issues I found (the most notable being missing mandate enforcement), followed by a conflict-free merge with |
I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed. |
Thanks for implementing this C++23 feature - it's expected to be an awesome Standard! 😹 😻 🚀 |
Implements P2505R5 Monadic Functions for
std::expected
Fixes #3210
Open question: The
transform
andtransform_error
specifications haveMandates
clauses similar to:Do we want
static_asserts
for these?