]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Do not use std::expected::value() in monadic ops (LWG 3938)
authorJonathan Wakely <jwakely@redhat.com>
Thu, 1 Jun 2023 10:16:49 +0000 (11:16 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 1 Jun 2023 15:06:15 +0000 (16:06 +0100)
commitfe94f8b7e022b7e154f6c47cc292d4463bddac5e
treee210a16c2df0d7b5b88f092daced5531c75a20b7
parentb7b255e77a271974479c34d1db3daafc04b920bc
libstdc++: Do not use std::expected::value() in monadic ops (LWG 3938)

The monadic operations in std::expected always check has_value() so we
can avoid the execptional path in value() and the assertions in error()
by accessing _M_val and _M_unex directly. This means that the monadic
operations no longer require _M_unex to be copyable so that it can be
thrown from value(), as modified by LWG 3938.

This also fixes two incorrect uses of std::move in transform(F&&)& and
transform(F&&) const& which I found while making these changes.

Now that move-only error types are supported, it's possible to properly
test the constraints that LWG 3877 added to and_then and transform. The
lwg3877.cc test now does that.

libstdc++-v3/ChangeLog:

* include/std/expected (expected::and_then, expected::or_else)
(expected::transform_error): Use _M_val and _M_unex instead of
calling value() and error(), as per LWG 3938.
(expected::transform): Likewise. Remove incorrect std::move
calls from lvalue overloads.
(expected<void, E>::and_then, expected<void, E>::or_else)
(expected<void, E>::transform): Use _M_unex instead of calling
error().
* testsuite/20_util/expected/lwg3877.cc: Add checks for and_then
and transform, and for std::expected<void, E>.
* testsuite/20_util/expected/lwg3938.cc: New test.
libstdc++-v3/include/std/expected
libstdc++-v3/testsuite/20_util/expected/lwg3877.cc
libstdc++-v3/testsuite/20_util/expected/lwg3938.cc [new file with mode: 0644]