| 1 |
From 14c70a678a76457562a1be33d1809463dd3bf6e8 Mon Sep 17 00:00:00 2001 |
| 2 |
From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= <[email protected]> |
| 3 |
Date: Wed, 19 Feb 2025 12:12:29 +0100 |
| 4 |
Subject: [PATCH 1/3] Rename tl::unexpected::value() to tl::unexpected::error() |
| 5 |
|
| 6 |
Fixes #149 |
| 7 |
--- |
| 8 |
include/tl/expected.hpp | 52 ++++++++++++++++++++--------------------- |
| 9 |
tests/constructors.cpp | 20 ++++++++++++++++ |
| 10 |
tests/issues.cpp | 2 +- |
| 11 |
tests/observers.cpp | 12 ++++++++++ |
| 12 |
tests/relops.cpp | 25 ++++++++++++++++++++ |
| 13 |
5 files changed, 84 insertions(+), 27 deletions(-) |
| 14 |
|
| 15 |
diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp |
| 16 |
index 3c22c5c..9d3a5e1 100644 |
| 17 |
--- a/include/tl/expected.hpp |
| 18 |
+++ b/include/tl/expected.hpp |
| 19 |
@@ -165,10 +165,10 @@ public: |
| 20 |
constexpr explicit unexpected(std::initializer_list<U> l, Args &&...args) |
| 21 |
: m_val(l, std::forward<Args>(args)...) {} |
| 22 |
|
| 23 |
- constexpr const E &value() const & { return m_val; } |
| 24 |
- TL_EXPECTED_11_CONSTEXPR E &value() & { return m_val; } |
| 25 |
- TL_EXPECTED_11_CONSTEXPR E &&value() && { return std::move(m_val); } |
| 26 |
- constexpr const E &&value() const && { return std::move(m_val); } |
| 27 |
+ constexpr const E &error() const & { return m_val; } |
| 28 |
+ TL_EXPECTED_11_CONSTEXPR E &error() & { return m_val; } |
| 29 |
+ TL_EXPECTED_11_CONSTEXPR E &&error() && { return std::move(m_val); } |
| 30 |
+ constexpr const E &&error() const && { return std::move(m_val); } |
| 31 |
|
| 32 |
private: |
| 33 |
E m_val; |
| 34 |
@@ -180,27 +180,27 @@ template <class E> unexpected(E) -> unexpected<E>; |
| 35 |
|
| 36 |
template <class E> |
| 37 |
constexpr bool operator==(const unexpected<E> &lhs, const unexpected<E> &rhs) { |
| 38 |
- return lhs.value() == rhs.value(); |
| 39 |
+ return lhs.error() == rhs.error(); |
| 40 |
} |
| 41 |
template <class E> |
| 42 |
constexpr bool operator!=(const unexpected<E> &lhs, const unexpected<E> &rhs) { |
| 43 |
- return lhs.value() != rhs.value(); |
| 44 |
+ return lhs.error() != rhs.error(); |
| 45 |
} |
| 46 |
template <class E> |
| 47 |
constexpr bool operator<(const unexpected<E> &lhs, const unexpected<E> &rhs) { |
| 48 |
- return lhs.value() < rhs.value(); |
| 49 |
+ return lhs.error() < rhs.error(); |
| 50 |
} |
| 51 |
template <class E> |
| 52 |
constexpr bool operator<=(const unexpected<E> &lhs, const unexpected<E> &rhs) { |
| 53 |
- return lhs.value() <= rhs.value(); |
| 54 |
+ return lhs.error() <= rhs.error(); |
| 55 |
} |
| 56 |
template <class E> |
| 57 |
constexpr bool operator>(const unexpected<E> &lhs, const unexpected<E> &rhs) { |
| 58 |
- return lhs.value() > rhs.value(); |
| 59 |
+ return lhs.error() > rhs.error(); |
| 60 |
} |
| 61 |
template <class E> |
| 62 |
constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) { |
| 63 |
- return lhs.value() >= rhs.value(); |
| 64 |
+ return lhs.error() >= rhs.error(); |
| 65 |
} |
| 66 |
|
| 67 |
template <class E> |
| 68 |
@@ -1582,7 +1582,7 @@ public: |
| 69 |
detail::enable_if_t<!std::is_convertible<const G &, E>::value> * = |
| 70 |
nullptr> |
| 71 |
explicit constexpr expected(const unexpected<G> &e) |
| 72 |
- : impl_base(unexpect, e.value()), |
| 73 |
+ : impl_base(unexpect, e.error()), |
| 74 |
ctor_base(detail::default_constructor_tag{}) {} |
| 75 |
|
| 76 |
template < |
| 77 |
@@ -1591,7 +1591,7 @@ public: |
| 78 |
nullptr, |
| 79 |
detail::enable_if_t<std::is_convertible<const G &, E>::value> * = nullptr> |
| 80 |
constexpr expected(unexpected<G> const &e) |
| 81 |
- : impl_base(unexpect, e.value()), |
| 82 |
+ : impl_base(unexpect, e.error()), |
| 83 |
ctor_base(detail::default_constructor_tag{}) {} |
| 84 |
|
| 85 |
template < |
| 86 |
@@ -1600,7 +1600,7 @@ public: |
| 87 |
detail::enable_if_t<!std::is_convertible<G &&, E>::value> * = nullptr> |
| 88 |
explicit constexpr expected(unexpected<G> &&e) noexcept( |
| 89 |
std::is_nothrow_constructible<E, G &&>::value) |
| 90 |
- : impl_base(unexpect, std::move(e.value())), |
| 91 |
+ : impl_base(unexpect, std::move(e.error())), |
| 92 |
ctor_base(detail::default_constructor_tag{}) {} |
| 93 |
|
| 94 |
template < |
| 95 |
@@ -1609,7 +1609,7 @@ public: |
| 96 |
detail::enable_if_t<std::is_convertible<G &&, E>::value> * = nullptr> |
| 97 |
constexpr expected(unexpected<G> &&e) noexcept( |
| 98 |
std::is_nothrow_constructible<E, G &&>::value) |
| 99 |
- : impl_base(unexpect, std::move(e.value())), |
| 100 |
+ : impl_base(unexpect, std::move(e.error())), |
| 101 |
ctor_base(detail::default_constructor_tag{}) {} |
| 102 |
|
| 103 |
template <class... Args, |
| 104 |
@@ -2017,46 +2017,46 @@ public: |
| 105 |
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> |
| 106 |
TL_EXPECTED_11_CONSTEXPR const U &value() const & { |
| 107 |
if (!has_value()) |
| 108 |
- detail::throw_exception(bad_expected_access<E>(err().value())); |
| 109 |
+ detail::throw_exception(bad_expected_access<E>(err().error())); |
| 110 |
return val(); |
| 111 |
} |
| 112 |
template <class U = T, |
| 113 |
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> |
| 114 |
TL_EXPECTED_11_CONSTEXPR U &value() & { |
| 115 |
if (!has_value()) |
| 116 |
- detail::throw_exception(bad_expected_access<E>(err().value())); |
| 117 |
+ detail::throw_exception(bad_expected_access<E>(err().error())); |
| 118 |
return val(); |
| 119 |
} |
| 120 |
template <class U = T, |
| 121 |
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> |
| 122 |
TL_EXPECTED_11_CONSTEXPR const U &&value() const && { |
| 123 |
if (!has_value()) |
| 124 |
- detail::throw_exception(bad_expected_access<E>(std::move(err()).value())); |
| 125 |
+ detail::throw_exception(bad_expected_access<E>(std::move(err()).error())); |
| 126 |
return std::move(val()); |
| 127 |
} |
| 128 |
template <class U = T, |
| 129 |
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> |
| 130 |
TL_EXPECTED_11_CONSTEXPR U &&value() && { |
| 131 |
if (!has_value()) |
| 132 |
- detail::throw_exception(bad_expected_access<E>(std::move(err()).value())); |
| 133 |
+ detail::throw_exception(bad_expected_access<E>(std::move(err()).error())); |
| 134 |
return std::move(val()); |
| 135 |
} |
| 136 |
|
| 137 |
constexpr const E &error() const & { |
| 138 |
TL_ASSERT(!has_value()); |
| 139 |
- return err().value(); |
| 140 |
+ return err().error(); |
| 141 |
} |
| 142 |
TL_EXPECTED_11_CONSTEXPR E &error() & { |
| 143 |
TL_ASSERT(!has_value()); |
| 144 |
- return err().value(); |
| 145 |
+ return err().error(); |
| 146 |
} |
| 147 |
constexpr const E &&error() const && { |
| 148 |
TL_ASSERT(!has_value()); |
| 149 |
- return std::move(err().value()); |
| 150 |
+ return std::move(err().error()); |
| 151 |
} |
| 152 |
TL_EXPECTED_11_CONSTEXPR E &&error() && { |
| 153 |
TL_ASSERT(!has_value()); |
| 154 |
- return std::move(err().value()); |
| 155 |
+ return std::move(err().error()); |
| 156 |
} |
| 157 |
|
| 158 |
template <class U> constexpr T value_or(U &&v) const & { |
| 159 |
@@ -2446,19 +2446,19 @@ constexpr bool operator!=(const U &v, const expected<T, E> &x) { |
| 160 |
|
| 161 |
template <class T, class E> |
| 162 |
constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) { |
| 163 |
- return x.has_value() ? false : x.error() == e.value(); |
| 164 |
+ return x.has_value() ? false : x.error() == e.error(); |
| 165 |
} |
| 166 |
template <class T, class E> |
| 167 |
constexpr bool operator==(const unexpected<E> &e, const expected<T, E> &x) { |
| 168 |
- return x.has_value() ? false : x.error() == e.value(); |
| 169 |
+ return x.has_value() ? false : x.error() == e.error(); |
| 170 |
} |
| 171 |
template <class T, class E> |
| 172 |
constexpr bool operator!=(const expected<T, E> &x, const unexpected<E> &e) { |
| 173 |
- return x.has_value() ? true : x.error() != e.value(); |
| 174 |
+ return x.has_value() ? true : x.error() != e.error(); |
| 175 |
} |
| 176 |
template <class T, class E> |
| 177 |
constexpr bool operator!=(const unexpected<E> &e, const expected<T, E> &x) { |
| 178 |
- return x.has_value() ? true : x.error() != e.value(); |
| 179 |
+ return x.has_value() ? true : x.error() != e.error(); |
| 180 |
} |
| 181 |
|
| 182 |
template <class T, class E, |
| 183 |
diff --git a/tests/constructors.cpp b/tests/constructors.cpp |
| 184 |
index df168ba..10b96de 100644 |
| 185 |
--- a/tests/constructors.cpp |
| 186 |
+++ b/tests/constructors.cpp |
| 187 |
@@ -131,4 +131,24 @@ TEST_CASE("Constructors", "[constructors]") { |
| 188 |
REQUIRE(!e); |
| 189 |
REQUIRE(e.error() == 42); |
| 190 |
} |
| 191 |
+ |
| 192 |
+ { |
| 193 |
+ constexpr tl::unexpected<char> u('s'); |
| 194 |
+ tl::expected<int, int> e(u); |
| 195 |
+ REQUIRE(e.error() == 's'); |
| 196 |
+ } |
| 197 |
+ |
| 198 |
+ { |
| 199 |
+ struct value { |
| 200 |
+ constexpr explicit value(char v) : val(v) {} |
| 201 |
+ char val; |
| 202 |
+ }; |
| 203 |
+ |
| 204 |
+ constexpr tl::unexpected<char> u('s'); |
| 205 |
+ tl::expected<int, value> e1(u); |
| 206 |
+ REQUIRE(e1.error().val == 's'); |
| 207 |
+ |
| 208 |
+ tl::expected<int, value> e2(tl::unexpected<char>('s')); |
| 209 |
+ REQUIRE(e2.error().val == 's'); |
| 210 |
+ } |
| 211 |
} |
| 212 |
diff --git a/tests/issues.cpp b/tests/issues.cpp |
| 213 |
index f929099..9efe14b 100644 |
| 214 |
--- a/tests/issues.cpp |
| 215 |
+++ b/tests/issues.cpp |
| 216 |
@@ -166,7 +166,7 @@ TEST_CASE("Issue 122", "[issues.122]") { |
| 217 |
#ifdef __cpp_deduction_guides |
| 218 |
TEST_CASE("Issue 89", "[issues.89]") { |
| 219 |
auto s = tl::unexpected("Some string"); |
| 220 |
- REQUIRE(s.value() == std::string("Some string")); |
| 221 |
+ REQUIRE(s.error() == std::string("Some string")); |
| 222 |
} |
| 223 |
#endif |
| 224 |
|
| 225 |
diff --git a/tests/observers.cpp b/tests/observers.cpp |
| 226 |
index 5d8473c..cff3e9f 100644 |
| 227 |
--- a/tests/observers.cpp |
| 228 |
+++ b/tests/observers.cpp |
| 229 |
@@ -34,3 +34,15 @@ TEST_CASE("Observers", "[observers]") { |
| 230 |
REQUIRE(o4->been_moved); |
| 231 |
REQUIRE(!o5.been_moved); |
| 232 |
} |
| 233 |
+ |
| 234 |
+#ifdef TL_EXPECTED_EXCEPTIONS_ENABLED |
| 235 |
+TEST_CASE("Observers invalid access", "[observers]") { |
| 236 |
+ tl::expected<int, char> err(tl::make_unexpected('!')); |
| 237 |
+ |
| 238 |
+ REQUIRE_THROWS_AS(err.value(), tl::bad_expected_access<char>); |
| 239 |
+ REQUIRE_THROWS_AS(std::as_const(err).value(), tl::bad_expected_access<char>); |
| 240 |
+ REQUIRE_THROWS_AS(std::move(std::as_const(err)).value(), |
| 241 |
+ tl::bad_expected_access<char>); |
| 242 |
+ REQUIRE_THROWS_AS(std::move(err).value(), tl::bad_expected_access<char>); |
| 243 |
+} |
| 244 |
+#endif |
| 245 |
diff --git a/tests/relops.cpp b/tests/relops.cpp |
| 246 |
index 99dee5f..b873b6a 100644 |
| 247 |
--- a/tests/relops.cpp |
| 248 |
+++ b/tests/relops.cpp |
| 249 |
@@ -15,3 +15,28 @@ TEST_CASE("Relational operators", "[relops]") { |
| 250 |
|
| 251 |
REQUIRE(o6 == o6); |
| 252 |
} |
| 253 |
+ |
| 254 |
+TEST_CASE("Relational operators unexpected", "[relops]") { |
| 255 |
+ tl::unexpected<int> zero(0); |
| 256 |
+ tl::unexpected<int> one(1); |
| 257 |
+ |
| 258 |
+ REQUIRE(one == one); |
| 259 |
+ REQUIRE(one != zero); |
| 260 |
+ REQUIRE(zero < one); |
| 261 |
+ REQUIRE(zero <= one); |
| 262 |
+ REQUIRE(one <= one); |
| 263 |
+ REQUIRE(one > zero); |
| 264 |
+ REQUIRE(one >= zero); |
| 265 |
+ REQUIRE(one >= one); |
| 266 |
+} |
| 267 |
+ |
| 268 |
+TEST_CASE("Relational operators expected vs unexpected", "[relops]") { |
| 269 |
+ tl::expected<int, int> ezero(tl::unexpect, 0); |
| 270 |
+ tl::unexpected<int> uzero(0); |
| 271 |
+ tl::unexpected<int> uone(1); |
| 272 |
+ |
| 273 |
+ REQUIRE(ezero == uzero); |
| 274 |
+ REQUIRE(ezero != uone); |
| 275 |
+ REQUIRE(uzero == ezero); |
| 276 |
+ REQUIRE(uone != ezero); |
| 277 |
+} |
| 278 |
-- |
| 279 |
2.44.0.windows.1 |
| 280 |
|