libstdc++: Implement monadic operations for std::optional (P0798R8)
authorJonathan Wakely <jwakely@redhat.com>
Tue, 19 Oct 2021 10:06:56 +0000 (11:06 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 19 Oct 2021 14:01:16 +0000 (15:01 +0100)
commit82b2e4f8cf5a01c6724fe3f465a77ee03cfcaae2
treeebc6317967464146890f1a47aae196445dd0bb31
parent6920d5a1a2834e9c62d441b8f4c6186b01107d13
libstdc++: Implement monadic operations for std::optional (P0798R8)

Another new addition to the C++23 working draft.

The new member functions of std::optional are only defined for C++23,
but the new members of _Optional_payload_base are defined for C++20 so
that they can be used in non-propagating-cache in <ranges>. The
_Optional_payload_base::_M_construct member can also be used in
non-propagating-cache now, because it's constexpr since r12-4389.

There will be an LWG issue about the feature test macro, suggesting that
we should just bump the value of __cpp_lib_optional instead. I haven't
done that here, but it can be changed once consensus is reached on the
change.

libstdc++-v3/ChangeLog:

* include/std/optional (_Optional_payload_base::_Storage): Add
constructor taking a callable function to invoke.
(_Optional_payload_base::_M_apply): New function.
(__cpp_lib_monadic_optional): Define for C++23.
(optional::and_then, optional::transform, optional::or_else):
Define for C++23.
* include/std/ranges (__detail::__cached): Remove.
(__detail::__non_propagating_cache): Remove use of __cached for
contained value. Use _Optional_payload_base::_M_construct and
_Optional_payload_base::_M_apply to set the contained value.
* include/std/version (__cpp_lib_monadic_optional): Define.
* testsuite/20_util/optional/monadic/and_then.cc: New test.
* testsuite/20_util/optional/monadic/or_else.cc: New test.
* testsuite/20_util/optional/monadic/or_else_neg.cc: New test.
* testsuite/20_util/optional/monadic/transform.cc: New test.
* testsuite/20_util/optional/monadic/version.cc: New test.
libstdc++-v3/include/std/optional
libstdc++-v3/include/std/ranges
libstdc++-v3/include/std/version
libstdc++-v3/testsuite/20_util/optional/monadic/and_then.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/optional/monadic/or_else.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/optional/monadic/or_else_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/optional/monadic/transform.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/optional/monadic/version.cc [new file with mode: 0644]