| 1 |
From c6064c91aa84412d07a4f3948d19496e0f294cef 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 14:05:45 +0100 |
| 4 |
Subject: [PATCH 2/3] Require in_place_t with variadic unexpected ctors |
| 5 |
|
| 6 |
This makes unexpected ctors more consistent with the C++ standard |
| 7 |
--- |
| 8 |
include/tl/expected.hpp | 31 +++++----- |
| 9 |
tests/constructors.cpp | 123 ++++++++++++++++++++++++++++++++++++++++ |
| 10 |
2 files changed, 140 insertions(+), 14 deletions(-) |
| 11 |
|
| 12 |
diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp |
| 13 |
index 9d3a5e1..83920bd 100644 |
| 14 |
--- a/include/tl/expected.hpp |
| 15 |
+++ b/include/tl/expected.hpp |
| 16 |
@@ -156,13 +156,15 @@ public: |
| 17 |
|
| 18 |
template <class... Args, typename std::enable_if<std::is_constructible< |
| 19 |
E, Args &&...>::value>::type * = nullptr> |
| 20 |
- constexpr explicit unexpected(Args &&...args) |
| 21 |
+ constexpr explicit unexpected(in_place_t, Args &&...args) |
| 22 |
: m_val(std::forward<Args>(args)...) {} |
| 23 |
+ |
| 24 |
template < |
| 25 |
class U, class... Args, |
| 26 |
typename std::enable_if<std::is_constructible< |
| 27 |
E, std::initializer_list<U> &, Args &&...>::value>::type * = nullptr> |
| 28 |
- constexpr explicit unexpected(std::initializer_list<U> l, Args &&...args) |
| 29 |
+ constexpr explicit unexpected(in_place_t, std::initializer_list<U> l, |
| 30 |
+ Args &&...args) |
| 31 |
: m_val(l, std::forward<Args>(args)...) {} |
| 32 |
|
| 33 |
constexpr const E &error() const & { return m_val; } |
| 34 |
@@ -471,7 +473,7 @@ struct expected_storage_base { |
| 35 |
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = |
| 36 |
nullptr> |
| 37 |
constexpr explicit expected_storage_base(unexpect_t, Args &&...args) |
| 38 |
- : m_unexpect(std::forward<Args>(args)...), m_has_val(false) {} |
| 39 |
+ : m_unexpect(in_place, std::forward<Args>(args)...), m_has_val(false) {} |
| 40 |
|
| 41 |
template <class U, class... Args, |
| 42 |
detail::enable_if_t<std::is_constructible< |
| 43 |
@@ -479,7 +481,7 @@ struct expected_storage_base { |
| 44 |
constexpr explicit expected_storage_base(unexpect_t, |
| 45 |
std::initializer_list<U> il, |
| 46 |
Args &&...args) |
| 47 |
- : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} |
| 48 |
+ : m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {} |
| 49 |
|
| 50 |
~expected_storage_base() { |
| 51 |
if (m_has_val) { |
| 52 |
@@ -518,7 +520,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, true> { |
| 53 |
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = |
| 54 |
nullptr> |
| 55 |
constexpr explicit expected_storage_base(unexpect_t, Args &&...args) |
| 56 |
- : m_unexpect(std::forward<Args>(args)...), m_has_val(false) {} |
| 57 |
+ : m_unexpect(in_place, std::forward<Args>(args)...), m_has_val(false) {} |
| 58 |
|
| 59 |
template <class U, class... Args, |
| 60 |
detail::enable_if_t<std::is_constructible< |
| 61 |
@@ -526,7 +528,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, true> { |
| 62 |
constexpr explicit expected_storage_base(unexpect_t, |
| 63 |
std::initializer_list<U> il, |
| 64 |
Args &&...args) |
| 65 |
- : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} |
| 66 |
+ : m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {} |
| 67 |
|
| 68 |
expected_storage_base(const expected_storage_base &) = default; |
| 69 |
expected_storage_base(expected_storage_base &&) = default; |
| 70 |
@@ -563,7 +565,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, false> { |
| 71 |
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = |
| 72 |
nullptr> |
| 73 |
constexpr explicit expected_storage_base(unexpect_t, Args &&...args) |
| 74 |
- : m_unexpect(std::forward<Args>(args)...), m_has_val(false) {} |
| 75 |
+ : m_unexpect(in_place, std::forward<Args>(args)...), m_has_val(false) {} |
| 76 |
|
| 77 |
template <class U, class... Args, |
| 78 |
detail::enable_if_t<std::is_constructible< |
| 79 |
@@ -571,7 +573,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, false> { |
| 80 |
constexpr explicit expected_storage_base(unexpect_t, |
| 81 |
std::initializer_list<U> il, |
| 82 |
Args &&...args) |
| 83 |
- : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} |
| 84 |
+ : m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {} |
| 85 |
|
| 86 |
expected_storage_base(const expected_storage_base &) = default; |
| 87 |
expected_storage_base(expected_storage_base &&) = default; |
| 88 |
@@ -612,7 +614,7 @@ template <class T, class E> struct expected_storage_base<T, E, false, true> { |
| 89 |
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = |
| 90 |
nullptr> |
| 91 |
constexpr explicit expected_storage_base(unexpect_t, Args &&...args) |
| 92 |
- : m_unexpect(std::forward<Args>(args)...), m_has_val(false) {} |
| 93 |
+ : m_unexpect(in_place, std::forward<Args>(args)...), m_has_val(false) {} |
| 94 |
|
| 95 |
template <class U, class... Args, |
| 96 |
detail::enable_if_t<std::is_constructible< |
| 97 |
@@ -620,7 +622,8 @@ template <class T, class E> struct expected_storage_base<T, E, false, true> { |
| 98 |
constexpr explicit expected_storage_base(unexpect_t, |
| 99 |
std::initializer_list<U> il, |
| 100 |
Args &&...args) |
| 101 |
- : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} |
| 102 |
+ : m_unexpect(in_place, il, std::forward<Args>(args)...), |
| 103 |
+ m_has_val(false) {} |
| 104 |
|
| 105 |
expected_storage_base(const expected_storage_base &) = default; |
| 106 |
expected_storage_base(expected_storage_base &&) = default; |
| 107 |
@@ -656,7 +659,7 @@ template <class E> struct expected_storage_base<void, E, false, true> { |
| 108 |
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = |
| 109 |
nullptr> |
| 110 |
constexpr explicit expected_storage_base(unexpect_t, Args &&...args) |
| 111 |
- : m_unexpect(std::forward<Args>(args)...), m_has_val(false) {} |
| 112 |
+ : m_unexpect(in_place, std::forward<Args>(args)...), m_has_val(false) {} |
| 113 |
|
| 114 |
template <class U, class... Args, |
| 115 |
detail::enable_if_t<std::is_constructible< |
| 116 |
@@ -664,7 +667,7 @@ template <class E> struct expected_storage_base<void, E, false, true> { |
| 117 |
constexpr explicit expected_storage_base(unexpect_t, |
| 118 |
std::initializer_list<U> il, |
| 119 |
Args &&...args) |
| 120 |
- : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} |
| 121 |
+ : m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {} |
| 122 |
|
| 123 |
expected_storage_base(const expected_storage_base &) = default; |
| 124 |
expected_storage_base(expected_storage_base &&) = default; |
| 125 |
@@ -690,7 +693,7 @@ template <class E> struct expected_storage_base<void, E, false, false> { |
| 126 |
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = |
| 127 |
nullptr> |
| 128 |
constexpr explicit expected_storage_base(unexpect_t, Args &&...args) |
| 129 |
- : m_unexpect(std::forward<Args>(args)...), m_has_val(false) {} |
| 130 |
+ : m_unexpect(in_place, std::forward<Args>(args)...), m_has_val(false) {} |
| 131 |
|
| 132 |
template <class U, class... Args, |
| 133 |
detail::enable_if_t<std::is_constructible< |
| 134 |
@@ -698,7 +701,7 @@ template <class E> struct expected_storage_base<void, E, false, false> { |
| 135 |
constexpr explicit expected_storage_base(unexpect_t, |
| 136 |
std::initializer_list<U> il, |
| 137 |
Args &&...args) |
| 138 |
- : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} |
| 139 |
+ : m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {} |
| 140 |
|
| 141 |
expected_storage_base(const expected_storage_base &) = default; |
| 142 |
expected_storage_base(expected_storage_base &&) = default; |
| 143 |
diff --git a/tests/constructors.cpp b/tests/constructors.cpp |
| 144 |
index 10b96de..f24a46a 100644 |
| 145 |
--- a/tests/constructors.cpp |
| 146 |
+++ b/tests/constructors.cpp |
| 147 |
@@ -3,6 +3,7 @@ |
| 148 |
|
| 149 |
#include <type_traits> |
| 150 |
#include <vector> |
| 151 |
+#include <array> |
| 152 |
#include <string> |
| 153 |
|
| 154 |
struct takes_init_and_variadic { |
| 155 |
@@ -13,6 +14,27 @@ struct takes_init_and_variadic { |
| 156 |
: v(l), t(std::forward<Args>(args)...) {} |
| 157 |
}; |
| 158 |
|
| 159 |
+struct trivial_type { |
| 160 |
+ int x; |
| 161 |
+ int y; |
| 162 |
+ trivial_type(int _x, int _y) : x{_x}, y{_y} {} |
| 163 |
+ trivial_type(std::initializer_list<int> list) { |
| 164 |
+ auto it = list.begin(); |
| 165 |
+ x = *it; |
| 166 |
+ ++it; |
| 167 |
+ y = *it; |
| 168 |
+ } |
| 169 |
+}; |
| 170 |
+ |
| 171 |
+struct takes_init_and_variadic_trivial { |
| 172 |
+ trivial_type p; |
| 173 |
+ std::tuple<int, int> t; |
| 174 |
+ template <class... Args> |
| 175 |
+ takes_init_and_variadic_trivial(std::initializer_list<int> l, |
| 176 |
+ Args &&...args) |
| 177 |
+ : p(l), t(std::forward<Args>(args)...) {} |
| 178 |
+}; |
| 179 |
+ |
| 180 |
TEST_CASE("Constructors", "[constructors]") { |
| 181 |
{ |
| 182 |
tl::expected<int,int> e; |
| 183 |
@@ -38,6 +60,12 @@ TEST_CASE("Constructors", "[constructors]") { |
| 184 |
REQUIRE(e == 42); |
| 185 |
} |
| 186 |
|
| 187 |
+ { |
| 188 |
+ tl::expected<int, int> e(tl::in_place); |
| 189 |
+ REQUIRE(e); |
| 190 |
+ REQUIRE(e == 0); |
| 191 |
+ } |
| 192 |
+ |
| 193 |
{ |
| 194 |
tl::expected<std::vector<int>,int> e (tl::in_place, {0,1}); |
| 195 |
REQUIRE(e); |
| 196 |
@@ -52,6 +80,40 @@ TEST_CASE("Constructors", "[constructors]") { |
| 197 |
REQUIRE(std::get<1>(*e) == 1); |
| 198 |
} |
| 199 |
|
| 200 |
+ { |
| 201 |
+ tl::expected<int, std::tuple<int, int>> e(tl::unexpect, 0, 1); |
| 202 |
+ REQUIRE(!e); |
| 203 |
+ REQUIRE(std::get<0>(e.error()) == 0); |
| 204 |
+ REQUIRE(std::get<1>(e.error()) == 1); |
| 205 |
+ } |
| 206 |
+ |
| 207 |
+ { |
| 208 |
+ tl::expected<void, std::tuple<int, int>> e(tl::unexpect, 0, 1); |
| 209 |
+ REQUIRE(!e); |
| 210 |
+ REQUIRE(std::get<0>(e.error()) == 0); |
| 211 |
+ REQUIRE(std::get<1>(e.error()) == 1); |
| 212 |
+ } |
| 213 |
+ { |
| 214 |
+ tl::expected<void, std::vector<int>> e(tl::unexpect, 2, 1); |
| 215 |
+ REQUIRE(!e); |
| 216 |
+ REQUIRE(e.error()[0] == 1); |
| 217 |
+ REQUIRE(e.error()[1] == 1); |
| 218 |
+ } |
| 219 |
+ { |
| 220 |
+ tl::expected<std::vector<int>, std::vector<int>> e(tl::unexpect, 2, 1); |
| 221 |
+ REQUIRE(!e); |
| 222 |
+ REQUIRE(e.error()[0] == 1); |
| 223 |
+ REQUIRE(e.error()[1] == 1); |
| 224 |
+ } |
| 225 |
+ { |
| 226 |
+ tl::expected<std::vector<int>, takes_init_and_variadic> e(tl::unexpect, |
| 227 |
+ {0, 1}, 2, 3); |
| 228 |
+ REQUIRE(!e); |
| 229 |
+ REQUIRE(e.error().v[0] == 0); |
| 230 |
+ REQUIRE(e.error().v[1] == 1); |
| 231 |
+ REQUIRE(std::get<0>(e.error().t) == 2); |
| 232 |
+ REQUIRE(std::get<1>(e.error().t) == 3); |
| 233 |
+ } |
| 234 |
{ |
| 235 |
tl::expected<takes_init_and_variadic,int> e (tl::in_place, {0,1}, 2, 3); |
| 236 |
REQUIRE(e); |
| 237 |
@@ -60,6 +122,59 @@ TEST_CASE("Constructors", "[constructors]") { |
| 238 |
REQUIRE(std::get<0>(e->t) == 2); |
| 239 |
REQUIRE(std::get<1>(e->t) == 3); |
| 240 |
} |
| 241 |
+ { |
| 242 |
+ tl::expected<int, takes_init_and_variadic> e(tl::unexpect, {0, 1}, 2, 3); |
| 243 |
+ REQUIRE(!e); |
| 244 |
+ REQUIRE(e.error().v[0] == 0); |
| 245 |
+ REQUIRE(e.error().v[1] == 1); |
| 246 |
+ REQUIRE(std::get<0>(e.error().t) == 2); |
| 247 |
+ REQUIRE(std::get<1>(e.error().t) == 3); |
| 248 |
+ } |
| 249 |
+ { |
| 250 |
+ tl::expected<int, takes_init_and_variadic_trivial> e(tl::unexpect, {0, 1}, 2, 3); |
| 251 |
+ REQUIRE(!e); |
| 252 |
+ REQUIRE(e.error().p.x == 0); |
| 253 |
+ REQUIRE(e.error().p.y == 1); |
| 254 |
+ REQUIRE(std::get<0>(e.error().t) == 2); |
| 255 |
+ REQUIRE(std::get<1>(e.error().t) == 3); |
| 256 |
+ } |
| 257 |
+ { |
| 258 |
+ tl::expected<void, takes_init_and_variadic_trivial> e(tl::unexpect, |
| 259 |
+ {0, 1}, 2, 3); |
| 260 |
+ REQUIRE(!e); |
| 261 |
+ REQUIRE(e.error().p.x == 0); |
| 262 |
+ REQUIRE(e.error().p.y == 1); |
| 263 |
+ REQUIRE(std::get<0>(e.error().t) == 2); |
| 264 |
+ REQUIRE(std::get<1>(e.error().t) == 3); |
| 265 |
+ } |
| 266 |
+ { |
| 267 |
+ tl::expected<int, std::vector<int>> e(tl::unexpect, 2, 1); |
| 268 |
+ REQUIRE(!e); |
| 269 |
+ REQUIRE(e.error()[0] == 1); |
| 270 |
+ REQUIRE(e.error()[1] == 1); |
| 271 |
+ } |
| 272 |
+ { |
| 273 |
+ tl::expected<std::vector<int>, trivial_type> e(tl::unexpect, 1, 2); |
| 274 |
+ REQUIRE(!e); |
| 275 |
+ REQUIRE(e.error().x == 1); |
| 276 |
+ REQUIRE(e.error().y == 2); |
| 277 |
+ } |
| 278 |
+ { |
| 279 |
+ tl::expected<std::vector<int>, takes_init_and_variadic_trivial> e(tl::unexpect, {1, 2}, 3, 4); |
| 280 |
+ REQUIRE(!e); |
| 281 |
+ REQUIRE(e.error().p.x == 1); |
| 282 |
+ REQUIRE(e.error().p.y == 2); |
| 283 |
+ REQUIRE(std::get<0>(e.error().t) == 3); |
| 284 |
+ REQUIRE(std::get<1>(e.error().t) == 4); |
| 285 |
+ } |
| 286 |
+ { |
| 287 |
+ tl::expected<void, takes_init_and_variadic> e(tl::unexpect, {0, 1}, 2, 3); |
| 288 |
+ REQUIRE(!e); |
| 289 |
+ REQUIRE(e.error().v[0] == 0); |
| 290 |
+ REQUIRE(e.error().v[1] == 1); |
| 291 |
+ REQUIRE(std::get<0>(e.error().t) == 2); |
| 292 |
+ REQUIRE(std::get<1>(e.error().t) == 3); |
| 293 |
+ } |
| 294 |
|
| 295 |
{ |
| 296 |
tl::expected<int, int> e; |
| 297 |
@@ -152,3 +267,11 @@ TEST_CASE("Constructors", "[constructors]") { |
| 298 |
REQUIRE(e2.error().val == 's'); |
| 299 |
} |
| 300 |
} |
| 301 |
+ |
| 302 |
+TEST_CASE("Unexpected constructors", "[constructors]") { |
| 303 |
+ REQUIRE(tl::unexpected<int>(1).error() == 1); |
| 304 |
+ REQUIRE(tl::unexpected<int>(tl::in_place).error() == 0); |
| 305 |
+ REQUIRE(tl::unexpected<int>(tl::in_place, 1).error() == 1); |
| 306 |
+ REQUIRE(tl::unexpected<std::vector<int>>(tl::in_place, {1, 2, 3}).error() == |
| 307 |
+ std::vector<int>{1, 2, 3}); |
| 308 |
+} |
| 309 |
-- |
| 310 |
2.44.0.windows.1 |
| 311 |
|