libstdc++: Fix std::expected<void, E>::swap(expected&) [PR105154]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 8 Apr 2022 17:17:47 +0000 (18:17 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 8 Apr 2022 17:30:06 +0000 (18:30 +0100)
libstdc++-v3/ChangeLog:

PR libstdc++/105154
* include/std/expected (expected<void, E>::swap): Set
_M_has_value to false for objects that previously had a value.
* testsuite/20_util/expected/swap.cc: Fix test to check void
specialization.

libstdc++-v3/include/std/expected
libstdc++-v3/testsuite/20_util/expected/swap.cc

index 7b01a17..1864e86 100644 (file)
@@ -1104,6 +1104,7 @@ namespace __expected
                std::construct_at(__builtin_addressof(_M_unex),
                                  std::move(__x._M_unex)); // might throw
                std::destroy_at(__builtin_addressof(__x._M_unex));
+               _M_has_value = false;
                __x._M_has_value = true;
              }
          }
@@ -1115,6 +1116,7 @@ namespace __expected
                                  std::move(_M_unex)); // might throw
                std::destroy_at(__builtin_addressof(_M_unex));
                _M_has_value = true;
+               __x._M_has_value = false;
              }
            else
              {
index 1b3b8c5..745db65 100644 (file)
@@ -27,19 +27,19 @@ test_swap()
   VERIFY( e3.error() == 4 );
   VERIFY( e4.error() == 3 );
 
-  std::expected<int, int> v1(1), v2(2);
-  std::expected<int, int> v3(std::unexpect, 3), v4(std::unexpect, 4);
+  std::expected<void, int> v1, v2;
+  std::expected<void, int> v3(std::unexpect, 3), v4(std::unexpect, 4);
 
   swap(v1, v2);
-  VERIFY( v1.value() == 2 );
-  VERIFY( v2.value() == 1 );
+  VERIFY( v1.has_value() );
+  VERIFY( v2.has_value() );
   swap(v1, v3);
   VERIFY( ! v1.has_value() );
   VERIFY( v1.error() == 3 );
-  VERIFY( v3.value() == 2 );
+  VERIFY( v3.has_value() );
   swap(v1, v3);
   VERIFY( ! v3.has_value() );
-  VERIFY( v1.value() == 2 );
+  VERIFY( v1.has_value() );
   VERIFY( v3.error() == 3 );
   swap(v3, v4);
   VERIFY( ! v3.has_value() );