| 1 | // { dg-do compile { target c++23 } } |
| 2 | |
| 3 | // LWG 3938. Cannot use std::expected monadic ops with move-only error_type |
| 4 | |
| 5 | #include <expected> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | struct MoveOnly { |
| 9 | constexpr MoveOnly(int i) : i(i) { } |
| 10 | constexpr MoveOnly(MoveOnly&&) = default; |
| 11 | constexpr MoveOnly(const MoveOnly&& mo) : i(mo.i) { } |
| 12 | constexpr bool operator==(const MoveOnly&) const = default; |
| 13 | int i; |
| 14 | }; |
| 15 | |
| 16 | constexpr bool |
| 17 | test_and_then() |
| 18 | { |
| 19 | auto fun = [](int i) { return std::expected<long, MoveOnly>(i); }; |
| 20 | |
| 21 | std::expected<int, MoveOnly> good(9); |
| 22 | std::expected<long, MoveOnly> e1 = std::move(good).and_then(fun); |
| 23 | VERIFY( e1 == good ); |
| 24 | const auto& gooder = good; |
| 25 | std::expected<long, MoveOnly> e2 = std::move(gooder).and_then(fun); |
| 26 | VERIFY( e2 == gooder ); |
| 27 | |
| 28 | std::expected<int, MoveOnly> bad(std::unexpect, 99); |
| 29 | std::expected<long, MoveOnly> e3 = std::move(bad).and_then(fun); |
| 30 | VERIFY( e3 == bad ); |
| 31 | const auto& badder = bad; |
| 32 | std::expected<long, MoveOnly> e4 = std::move(badder).and_then(fun); |
| 33 | VERIFY( e4 == badder ); |
| 34 | |
| 35 | auto vun = [] { return std::expected<long, MoveOnly>(1); }; |
| 36 | std::expected<void, MoveOnly> vud; |
| 37 | std::expected<long, MoveOnly> e5 = std::move(vud).and_then(vun); |
| 38 | VERIFY( *e5 == 1 ); |
| 39 | const auto& vudder = vud; |
| 40 | std::expected<long, MoveOnly> e6 = std::move(vudder).and_then(vun); |
| 41 | VERIFY( *e6 == 1 ); |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | static_assert( test_and_then() ); |
| 47 | |
| 48 | constexpr bool |
| 49 | test_or_else() |
| 50 | { |
| 51 | auto fun = [](const MoveOnly&& mo) { return std::expected<int, long>(mo.i); }; |
| 52 | |
| 53 | std::expected<int, MoveOnly> good(9); |
| 54 | std::expected<int, long> e1 = std::move(good).or_else(fun); |
| 55 | VERIFY( e1 == good ); |
| 56 | const auto& gooder = good; |
| 57 | std::expected<int, long> e2 = std::move(gooder).or_else(fun); |
| 58 | VERIFY( e2 == gooder ); |
| 59 | |
| 60 | std::expected<int, MoveOnly> bad(std::unexpect, 99); |
| 61 | std::expected<int, long> e3 = std::move(bad).or_else(fun); |
| 62 | VERIFY( *e3 == 99 ); |
| 63 | const auto& badder = bad; |
| 64 | std::expected<int, long> e4 = std::move(badder).or_else(fun); |
| 65 | VERIFY( *e4 == 99 ); |
| 66 | |
| 67 | auto vun = [](const MoveOnly&& mo) { return std::expected<void, long>{}; }; |
| 68 | std::expected<void, MoveOnly> vud; |
| 69 | std::expected<void, long> e5 = std::move(vud).or_else(vun); |
| 70 | VERIFY( e5.has_value() ); |
| 71 | const auto& vudder = vud; |
| 72 | std::expected<void, long> e6 = std::move(vudder).or_else(vun); |
| 73 | VERIFY( e6.has_value() ); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | static_assert( test_or_else() ); |
| 79 | |
| 80 | constexpr bool |
| 81 | test_transform() |
| 82 | { |
| 83 | auto fun = [](int i) { return (long)i; }; |
| 84 | |
| 85 | std::expected<int, MoveOnly> good(9); |
| 86 | std::expected<long, MoveOnly> e1 = std::move(good).transform(fun); |
| 87 | VERIFY( e1 == good ); |
| 88 | const auto& gooder = good; |
| 89 | std::expected<long, MoveOnly> e2 = std::move(gooder).transform(fun); |
| 90 | VERIFY( e2 == gooder ); |
| 91 | |
| 92 | std::expected<int, MoveOnly> bad(std::unexpect, 99); |
| 93 | std::expected<long, MoveOnly> e3 = std::move(bad).transform(fun); |
| 94 | VERIFY( e3 == bad ); |
| 95 | const auto& badder = bad; |
| 96 | std::expected<long, MoveOnly> e4 = std::move(badder).transform(fun); |
| 97 | VERIFY( e4 == badder ); |
| 98 | |
| 99 | auto vun = []() { return 1L; }; |
| 100 | std::expected<void, MoveOnly> vud; |
| 101 | std::expected<long, MoveOnly> e5 = std::move(vud).transform(vun); |
| 102 | VERIFY( *e5 == 1 ); |
| 103 | const auto& vudder = vud; |
| 104 | std::expected<long, MoveOnly> e6 = std::move(vudder).transform(vun); |
| 105 | VERIFY( *e6 == 1 ); |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | static_assert( test_transform() ); |
| 111 | |
| 112 | constexpr bool |
| 113 | test_transform_error() |
| 114 | { |
| 115 | auto fun = [](const MoveOnly&& mo) { return (long)mo.i; }; |
| 116 | |
| 117 | std::expected<int, MoveOnly> good(9); |
| 118 | std::expected<int, long> e1 = std::move(good).transform_error(fun); |
| 119 | VERIFY( e1 == good ); |
| 120 | const auto& gooder = good; |
| 121 | std::expected<int, long> e2 = std::move(gooder).transform_error(fun); |
| 122 | VERIFY( e2 == gooder ); |
| 123 | |
| 124 | std::expected<int, MoveOnly> bad(std::unexpect, 99); |
| 125 | std::expected<int, long> e3 = std::move(bad).transform_error(fun); |
| 126 | VERIFY( e3.error() == 99 ); |
| 127 | const auto& badder = bad; |
| 128 | std::expected<int, long> e4 = std::move(badder).transform_error(fun); |
| 129 | VERIFY( e4.error() == 99 ); |
| 130 | |
| 131 | std::expected<void, MoveOnly> vud(std::unexpect, 1); |
| 132 | std::expected<void, long> e5 = std::move(vud).transform_error(fun); |
| 133 | VERIFY( e5.error() == 1 ); |
| 134 | const auto& vudder = vud; |
| 135 | std::expected<void, long> e6 = std::move(vudder).transform_error(fun); |
| 136 | VERIFY( e6.error() == 1 ); |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | static_assert( test_transform_error() ); |