Line data Source code
1 : /* An <expected> for -*- C++ -*- 14.
2 : Derived, in part, from the libstdc++ one, but mostly distinct.
3 : Copyright (C) 2026 The GNU Toolchain Authors.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_EXPECTED
22 : #define GCC_EXPECTED 1
23 :
24 : #include <utility>
25 : #include <type_traits>
26 : #include <initializer_list>
27 : #include <memory>
28 :
29 : #include "gbits/assert.h"
30 : #include "gbits/enable_special_members.h"
31 : #include "gbits/exception_defines.h"
32 : #include "gbits/extras.h"
33 : #include "gbits/inplace_tags.h"
34 : #include "gbits/invoke.h"
35 : #include "gbits/stl_construct.h"
36 :
37 : #ifdef GCC_BACKPORT_WANT_EXCEPTIONS
38 : # include <exception>
39 : #endif
40 :
41 : #include "expectedfwd"
42 :
43 : namespace gcc
44 : {
45 : // [expected]
46 : template<typename E>
47 : struct unexpected;
48 :
49 : struct unexpect_t
50 : {
51 : explicit unexpect_t () = default;
52 : };
53 : static constexpr unexpect_t unexpect{};
54 :
55 : namespace detail { namespace exp
56 : {
57 : template<typename T>
58 : struct is_unexpected : std::false_type {};
59 :
60 : template<typename E>
61 : struct is_unexpected<unexpected<E>> : std::true_type {};
62 :
63 : template<typename T>
64 : struct is_expected : std::false_type {};
65 :
66 : template<typename T, typename E>
67 : struct is_expected<expected<T, E>> : std::true_type {};
68 :
69 : // true_type if E can be contained in an unexpected{} or !has_value
70 : // expected.
71 : template<typename E>
72 : using can_be_unexpected_val = and_<
73 : std::is_object<E>,
74 : not_<std::is_array<E>>,
75 : not_<is_unexpected<E>>,
76 : not_<is_cv_qualified<E>>
77 : >;
78 :
79 : // true_type if non-void T can be contained in a has_value expected.
80 : template<typename T>
81 : using can_be_expected_val_2 = and_<
82 : std::is_object<T>,
83 : not_<std::is_array<T>>,
84 : not_<is_in_place<T>>,
85 : not_<is_unexpected<T>>,
86 : not_<std::is_same<T, unexpect_t>>
87 : >;
88 : template<typename T>
89 : using can_be_expected_val = can_be_expected_val_2<std::remove_cv_t<T>>;
90 :
91 : // 'bool' if T1 is equality-comparable with T2, and E1 with E2, SFINAE-fail
92 : // otherwise. Used for comparisons of expected types.
93 : template<typename T1, typename T2,
94 : typename E1, typename E2>
95 : using eql_2_return_t = detail::relop_return_t<
96 : decltype (std::declval<const T1&> () == std::declval<const T2&> ()),
97 : decltype (std::declval<const E1&> () == std::declval<const E2&> ())
98 : >;
99 :
100 : // Implements the "U is a specialization of expected and is_same_v<typename
101 : // U::error_type, E> is true." mandate where U=Candidate and E=WantedError.
102 : template<typename Candidate, typename WantedError>
103 : struct is_expected_with_error : std::false_type {};
104 :
105 : template<typename T, typename WantedError>
106 : struct is_expected_with_error<expected<T, WantedError>, WantedError>
107 : : std::true_type {};
108 :
109 : // Implements the "G is a specialization of expected and
110 : // is_same_v<typename G::value_type, T> is true." mandate, where
111 : // G=Candidate and T=WantedValue.
112 : template<typename Candidate, typename WantedValue>
113 : struct is_expected_with_value : std::false_type {};
114 :
115 : template<typename E, typename WantedValue>
116 : struct is_expected_with_value<expected<WantedValue, E>, WantedValue>
117 : : std::true_type {};
118 :
119 : // Shorthand.
120 : template<template<typename> class Trait, typename T, typename E>
121 : using trait_2 = and_<Trait<T>, Trait<E>>;
122 :
123 : // Tag type, use supplied invocable to populate 'val'.
124 : struct in_place_inv {};
125 : // Likewise, but for 'unex'.
126 : struct unexpect_inv {};
127 : }} // namespace detail::exp
128 :
129 : template<typename E>
130 : struct unexpected
131 : {
132 : // [expected.un.general]p2
133 : static_assert (detail::exp::can_be_unexpected_val<E>::value,
134 : "E not a valid type for unexpected<>");
135 :
136 : // [expected.un.cons]
137 : unexpected (const unexpected &) = default;
138 : unexpected (unexpected &&) = default;
139 :
140 : template<typename Err = E,
141 : typename = std::enable_if_t<and_<
142 : std::is_constructible<E, Err>,
143 : not_<is_in_place<Err>>,
144 : not_<std::is_same<remove_cvref_t<Err>, unexpected>>
145 : >::value>>
146 : constexpr explicit
147 : unexpected (Err &&err)
148 : : unex (std::forward<Err>(err))
149 : {}
150 :
151 : template<typename... Args,
152 : typename = std::enable_if_t<
153 : std::is_constructible<E, Args...>::value
154 : >>
155 : constexpr explicit
156 : unexpected (in_place_t, Args&&... args)
157 : : unex (std::forward<Args>(args)...)
158 : {}
159 :
160 : template<typename U, typename... Args,
161 : typename = std::enable_if_t<
162 : std::is_constructible<E, std::initializer_list<U>&, Args...>::value
163 : >>
164 : constexpr explicit unexpected (in_place_t,
165 : std::initializer_list<U> il, Args&&... args)
166 : : unex (il, std::forward<Args>(args)...)
167 : {}
168 :
169 : unexpected& operator= (const unexpected&) = default;
170 : unexpected& operator= (unexpected&&) = default;
171 :
172 : // [expected.un.obs]
173 : [[nodiscard]] constexpr const E &
174 : error () const & noexcept
175 : { return unex; }
176 :
177 : [[nodiscard]] constexpr E &
178 : error () & noexcept
179 : { return unex; }
180 :
181 : [[nodiscard]] constexpr E &&
182 : error () && noexcept
183 : { return std::move (unex); }
184 :
185 : [[nodiscard]] constexpr const E &&
186 : error () const && noexcept
187 : { return std::move (unex); }
188 :
189 : // [expected.un.swap]
190 : template<typename E_ = E>
191 : constexpr std::enable_if_t<g_is_swappable<E_>::value>
192 : swap (unexpected& other) noexcept (g_is_nothrow_swappable<E>::value)
193 : {
194 : using std::swap;
195 : swap (unex, other.unex);
196 : }
197 :
198 : template<typename E_ = E>
199 : friend constexpr std::enable_if_t<g_is_swappable<E_>::value>
200 : swap (unexpected &x, unexpected &y) noexcept (noexcept (x.swap (y)))
201 : { x.swap (y); }
202 :
203 : // [expected.un.eq]
204 : template<typename E2>
205 : [[nodiscard]] friend constexpr eql_return_t<E, E2>
206 : operator== (const unexpected &x, const unexpected<E2> &y)
207 : { return x.error () == y.error (); }
208 :
209 : private:
210 : template<typename, typename, bool>
211 : friend struct expected;
212 :
213 : E unex;
214 : };
215 :
216 : // EXTENSION: Permit deduction of 'unexpected' from value type. Eventually
217 : // to be replaced with a deduction guide.
218 : template<typename E>
219 : constexpr unexpected<std::decay_t<E>>
220 : make_unexpected (E &&e)
221 : { return unexpected<std::decay_t<E>> (std::forward<E> (e)); }
222 :
223 : #if __cpp_deduction_guides >= 201703L
224 : template<class E> unexpected (E) -> unexpected<E>;
225 : #endif
226 :
227 : #ifdef GCC_BACKPORT_WANT_EXCEPTIONS
228 : // Only implemented to permit running testsuites for <expected> against this
229 : // implementation. Never actually enabled.
230 :
231 : // [expected.void.bad]
232 : template<typename E>
233 : class bad_expected_access;
234 :
235 : #if __cpp_lib_constexpr_exceptions >= 202502L
236 : #define GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS constexpr
237 : #else
238 : #define GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
239 : #endif
240 :
241 : template<>
242 : class bad_expected_access<void> : public std::exception
243 : {
244 : protected:
245 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS bad_expected_access () noexcept { }
246 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
247 : bad_expected_access (const bad_expected_access&) noexcept = default;
248 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
249 : bad_expected_access (bad_expected_access&&) noexcept = default;
250 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
251 : bad_expected_access& operator= (const bad_expected_access&) noexcept = default;
252 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
253 : bad_expected_access& operator= (bad_expected_access&&) noexcept = default;
254 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
255 : ~bad_expected_access () = default;
256 :
257 : public:
258 :
259 : [[nodiscard]]
260 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS const char*
261 : what () const noexcept override
262 : { return "bad access to std::expected without expected value"; }
263 : };
264 :
265 : // [expected.bad]
266 : template<typename E>
267 : class bad_expected_access : public bad_expected_access<void> {
268 : public:
269 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS explicit
270 : bad_expected_access (E g_e) : m_unex (std::move (g_e)) { }
271 :
272 : // XXX const char* what () const noexcept override;
273 :
274 : [[nodiscard]]
275 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS E&
276 : error () & noexcept
277 : { return m_unex; }
278 :
279 : [[nodiscard]]
280 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS const E&
281 : error () const & noexcept
282 : { return m_unex; }
283 :
284 : [[nodiscard]]
285 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS E&&
286 : error () && noexcept
287 : { return std::move (m_unex); }
288 :
289 : [[nodiscard]]
290 : GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS const E&&
291 : error () const && noexcept
292 : { return std::move (m_unex); }
293 :
294 : private:
295 : E m_unex;
296 : };
297 : #undef GCC_BACKPORT_CONSTEXPR_BAD_EXPECTED_ACCESS
298 : #endif // GCC_BACKPORT_WANT_EXCEPTIONS
299 :
300 : namespace detail { namespace exp
301 : {
302 : struct in_value_t {};
303 : // Tag, construct value field.
304 : static constexpr in_value_t in_value;
305 : struct in_unex_t {};
306 : // Tag, construct unex field.
307 : static constexpr in_unex_t in_unex;
308 : struct in_unused_t {};
309 : // Tag, construct unused field.
310 : static constexpr in_unused_t in_unused;
311 :
312 : // Replaces T=void for storage, and acts as a placeholder when storage is
313 : // empty.
314 : struct void_storage {};
315 :
316 : #define GCC_EXP_STORAGE_2_COMMON \
317 : constexpr storage_2 () = default; \
318 : \
319 : template<typename... Args> \
320 : constexpr storage_2 (in_value_t, Args&&... args) \
321 : : m_value (std::forward<Args> (args)...) \
322 : , m_has_value (true) \
323 : {} \
324 : \
325 : template<typename... Args> \
326 : constexpr storage_2 (in_unex_t, Args&&... args) \
327 : : m_unex (std::forward<Args> (args)...) \
328 : , m_has_value (false) \
329 : {} \
330 : \
331 : constexpr storage_2 (in_unused_t) \
332 : : m_unused () \
333 : /* To appease constexpr; always overwritten. */ \
334 : , m_has_value (false) \
335 : {} \
336 : \
337 : template<typename Fn> \
338 : constexpr storage_2 (in_place_inv, Fn &&fn) \
339 : : m_value (std::forward<Fn> (fn) ()) \
340 : , m_has_value (true) \
341 : {} \
342 : \
343 : template<typename Fn> \
344 : constexpr storage_2 (unexpect_inv, Fn &&fn) \
345 : : m_unex (std::forward<Fn> (fn) ()) \
346 : , m_has_value (false) \
347 : {} \
348 : \
349 : GCC_BACKPORT20_CONSTEXPR void \
350 : clear_storage () \
351 : { \
352 : if (this->m_has_value) \
353 : gcc::destroy_at (std::addressof (this->m_value)); \
354 : else \
355 : gcc::destroy_at (std::addressof (this->m_unex)); \
356 : }
357 :
358 : // Container holding the contents of the expected. Provides the
359 : // destructor.
360 : template<typename T, typename E,
361 : bool is_trivial = trait_2<std::is_trivially_destructible,
362 : T, E>::value>
363 : struct storage_2
364 : {
365 : /* Non-trivially destructible T or E. Need a hand-written
366 : destructor. */
367 : GCC_BACKPORT20_CONSTEXPR
368 : ~storage_2 ()
369 : { clear_storage (); }
370 :
371 : GCC_EXP_STORAGE_2_COMMON
372 :
373 : union {
374 : void_storage m_unused;
375 : std::remove_cv_t<T> m_value;
376 : E m_unex;
377 : };
378 : bool m_has_value;
379 : };
380 :
381 : template<typename T, typename E>
382 : struct storage_2<T, E, true>
383 : {
384 0 : GCC_EXP_STORAGE_2_COMMON
385 :
386 : union {
387 : void_storage m_unused;
388 : std::remove_cv_t<T> m_value;
389 : E m_unex;
390 : };
391 : bool m_has_value;
392 : };
393 : #undef GCC_EXP_STORAGE_2_COMMON
394 :
395 : // This layer provides some utilities.
396 : template<typename T, typename E>
397 : struct storage_1 : storage_2<T, E>
398 : {
399 0 : using storage_2<T, E>::storage_2;
400 :
401 : // Getters preserving const-ness and ref-ness of *this.
402 : #define GCC_EXP_STORAGE_1_DO_GETTER(type, member, name) \
403 : constexpr type &name () & noexcept \
404 : { return this->member; } \
405 : constexpr const type &name () const & noexcept \
406 : { return this->member; } \
407 : constexpr type &&name () && noexcept \
408 : { return std::move (this->member); } \
409 : constexpr const type &&name () const && noexcept \
410 : { return std::move (this->member); }
411 :
412 : GCC_EXP_STORAGE_1_DO_GETTER (T, m_value, value)
413 : GCC_EXP_STORAGE_1_DO_GETTER (E, m_unex, unex)
414 : #undef GCC_EXP_STORAGE_1_DO_GETTER
415 :
416 :
417 : // Helpers, for initial construction.
418 : template<typename... Args>
419 : constexpr void
420 : construct_value (Args&&... args)
421 : {
422 : gcc::construct_at (std::addressof (this->m_value),
423 : std::forward<Args> (args)...);
424 : this->m_has_value = true;
425 : }
426 :
427 : template<typename... Args>
428 : constexpr void
429 : construct_unex (Args&&... args)
430 : {
431 : gcc::construct_at (std::addressof (this->m_unex),
432 : std::forward<Args> (args)...);
433 : this->m_has_value = false;
434 : }
435 :
436 : // Copy/move construct our value/unex field based on other storage.
437 : // Presumes 'm_unused' is active right now.
438 : template<typename Other>
439 : constexpr
440 : std::enable_if_t<std::is_convertible<std::remove_reference_t<Other> *,
441 : const volatile storage_1 *>::value>
442 : construct (Other &&other)
443 : {
444 : if (other.m_has_value)
445 : this->construct_value (std::forward<Other> (other).value ());
446 : else
447 : this->construct_unex (std::forward<Other> (other).unex ());
448 : }
449 :
450 : // reinit-expected exposition-only helper.
451 :
452 : // Pick branch of reinit-expected helper.
453 : template<typename T2, typename U, typename... Args>
454 : static constexpr int
455 : pick_reinit_expected ()
456 : {
457 : if (std::is_same<U, void_storage>::value)
458 : // We're implementing expected<[cv] void, E> by reusing the storage
459 : // for expected<T, E> but giving it a dummy empty struct.
460 : //
461 : // However, doing this naively runs into a problem: if E cannot be
462 : // nothrow-constructed but can be nothrow-moved, reinit_expected will
463 : // construct a temporary E and move it into place, rather than
464 : // constructing it directly as [expected.void.assign] says.
465 : //
466 : // You may think we can just use branch 1 always then, but that
467 : // leaves us with a destroyed m_value and no m_unex, and with
468 : // m_has_value = true, so accesses down the line can trigger issues.
469 : //
470 : // In reality, we don't use exceptions in GCC. So, whatever, we can
471 : // take the higher-overhead branch.
472 : return 3;
473 : else if (std::is_nothrow_constructible<T2, Args...>::value)
474 : return 1;
475 : else if (std::is_nothrow_move_constructible<T2>::value)
476 : return 2;
477 : else
478 : return 3;
479 : }
480 :
481 : template<typename T2, typename U, typename... Args>
482 : GCC_BACKPORT20_CONSTEXPR void
483 : reinit_expected_ (std::integral_constant<int, 1>,
484 : T2& newval, U& oldval, Args&&... args)
485 : {
486 : gcc::destroy_at (std::addressof (oldval));
487 : gcc::construct_at (std::addressof (newval),
488 : std::forward<Args> (args)...);
489 : }
490 :
491 : template<typename T2, typename U, typename... Args>
492 : GCC_BACKPORT20_CONSTEXPR void
493 : reinit_expected_ (std::integral_constant<int, 2>,
494 : T2& newval, U& oldval, Args&&... args)
495 : {
496 : T2 tmp (std::forward<Args> (args)...);
497 : gcc::destroy_at (std::addressof (oldval));
498 : gcc::construct_at (std::addressof (newval), std::move (tmp));
499 : }
500 :
501 : template<typename T2, typename U, typename... Args>
502 : GCC_BACKPORT20_CONSTEXPR void
503 : reinit_expected_ (std::integral_constant<int, 3>,
504 : T2& newval, U& oldval, Args&&... args)
505 : {
506 : U tmp (std::move (oldval));
507 : gcc::destroy_at (std::addressof (oldval));
508 : g_try
509 : {
510 : gcc::construct_at (std::addressof (newval),
511 : std::forward<Args> (args)...);
512 : }
513 : g_catch (...)
514 : {
515 : gcc::construct_at (std::addressof (oldval),
516 : std::move (tmp));
517 : g_throw_exception_again;
518 : }
519 : }
520 :
521 : template<typename T2, typename U, typename... Args>
522 : GCC_BACKPORT20_CONSTEXPR void
523 : reinit_expected (T2& newval, U& oldval, Args&&... args)
524 : {
525 : constexpr int branch = pick_reinit_expected<T2, U, Args...>();
526 : reinit_expected_ (std::integral_constant<int, branch> (),
527 : newval, oldval, std::forward<Args> (args)...);
528 : }
529 :
530 : // Assign m_value.
531 : template<typename U>
532 : GCC_BACKPORT20_CONSTEXPR void
533 : assign_value (U &&v)
534 : {
535 : if (this->m_has_value)
536 : // "If has_value() is true, equivalent to: val = std::forward<U>(v);"
537 : this->m_value = std::forward<U> (v);
538 : else
539 : // "Otherwise, equivalent to: reinit-expected(val, unex,
540 : // std::forward<U>(v)); has_val = true;"
541 : {
542 : reinit_expected (this->m_value, this->m_unex, std::forward<U> (v));
543 : this->m_has_value = true;
544 : }
545 : }
546 :
547 : // Assign m_unex.
548 : template<typename Other>
549 : GCC_BACKPORT20_CONSTEXPR void
550 : assign_unex (Other &&other)
551 : {
552 : if (this->m_has_value)
553 : // "If has_value() is true, equivalent to: reinit-expected (unex, val,
554 : // std::forward<GF>(e.error())); has_val = false;"
555 : {
556 : // Must clear value first.
557 : reinit_expected (this->m_unex, this->m_value,
558 : std::forward<Other> (other));
559 : this->m_has_value = false;
560 : }
561 : else
562 : // "Otherwise, equivalent to: unex = std::forward<GF>(e.error());"
563 : this->m_unex = std::forward<Other> (other);
564 :
565 : // Note that the std::forward<GF> (e.error ()) bit is hoisted to the
566 : // caller.
567 : }
568 :
569 : // Non-trivial move/copy assignment implementation. OTHER must be a
570 : // storage_1. Called by the respective special members.
571 : template<typename Other>
572 : GCC_BACKPORT20_CONSTEXPR
573 : std::enable_if_t<std::is_convertible<std::remove_reference_t<Other> *,
574 : const volatile storage_1 *>::value>
575 : assign (Other &&other)
576 : {
577 : if (this->m_has_value && other.m_has_value)
578 : // "If this->has_value() && rhs.has_value() is true, equivalent to
579 : // val = *rhs." / "If this->has_value() && rhs.has_value() is true,
580 : // equivalent to val = std::move(*rhs)."
581 : this->m_value = std::forward<Other> (other).value ();
582 : else if (this->m_has_value)
583 : // "Otherwise, if this->has_value() is true, equivalent to:
584 : // reinit-expected(unex, val, rhs.error())" / "Otherwise, if
585 : // this->has_value() is true, equivalent to: reinit-expected(unex,
586 : // val, std::move(rhs.error()))"
587 : reinit_expected (this->m_unex, this->m_value,
588 : std::forward<Other> (other).unex ());
589 : else if (other.m_has_value)
590 : // "Otherwise, if rhs.has_value() is true, equivalent to:
591 : // reinit-expected(val, unex, *rhs)" / "Otherwise, if rhs.has_value()
592 : // is true, equivalent to: reinit-expected(val, unex,
593 : // std::move(*rhs))"
594 : reinit_expected (this->m_value, this->m_unex,
595 : std::forward<Other> (other).value ());
596 : else
597 : // "Otherwise, equivalent to unex = std::move(rhs.error())."
598 : this->m_unex = std::forward<Other> (other).unex ();
599 : // "Then, if no exception was thrown, equivalent to: has_val =
600 : // rhs.has_value(); return *this;"
601 : this->m_has_value = other.m_has_value;
602 : }
603 :
604 : // [expected.object.swap]p2 "For the case where rhs.has_value() is false
605 : // and this->has_value() is true", without the final 'has_val'
606 : // reassignments.
607 : template<typename E_ = E>
608 : GCC_BACKPORT20_CONSTEXPR
609 : std::enable_if_t<std::is_nothrow_move_constructible<E_>::value>
610 : swap_1 (T &val, E &unex,
611 : T &rhs_val, E &rhs_unex)
612 : {
613 : E tmp (std::move (rhs_unex));
614 : gcc::destroy_at (std::addressof (rhs_unex));
615 : g_try
616 : {
617 : gcc::construct_at (std::addressof (rhs_val), std::move (val));
618 : gcc::destroy_at (std::addressof (val));
619 : gcc::construct_at (std::addressof (unex), std::move (tmp));
620 : }
621 : g_catch (...)
622 : {
623 : gcc::construct_at (std::addressof (rhs_unex), std::move (tmp));
624 : g_throw_exception_again;
625 : }
626 : }
627 :
628 : template<typename E_ = E>
629 : GCC_BACKPORT20_CONSTEXPR
630 : std::enable_if_t<!std::is_nothrow_move_constructible<E_>::value>
631 : swap_1 (T &val, E &unex,
632 : T &rhs_val, E &rhs_unex)
633 : {
634 : static_assert (std::is_nothrow_move_constructible<T>::value,
635 : "Can't swap without a nothrow-move-constructible T or E"
636 : ", is a constraint missing on swap?");
637 : T tmp (std::move (val));
638 : gcc::destroy_at (std::addressof (val));
639 : g_try
640 : {
641 : gcc::construct_at (std::addressof (unex), std::move (rhs_unex));
642 : gcc::destroy_at (std::addressof (rhs_unex));
643 : gcc::construct_at (std::addressof (rhs_val), std::move (tmp));
644 : }
645 : g_catch (...)
646 : {
647 : gcc::construct_at (std::addressof (val), std::move (tmp));
648 : g_throw_exception_again;
649 : }
650 : }
651 :
652 : GCC_BACKPORT20_CONSTEXPR void
653 : _swap (storage_1 &other)
654 : {
655 : using std::swap;
656 :
657 : if (this->m_has_value && other.m_has_value)
658 : swap (this->m_value, other.m_value);
659 : else if (!this->m_has_value && !other.m_has_value)
660 : swap (this->m_unex, other.m_unex);
661 : else if (!this->m_has_value && other.m_has_value)
662 : other._swap (*this);
663 : else
664 : {
665 : swap_1 (this->m_value, this->m_unex, other.m_value, other.m_unex);
666 : this->m_has_value = false;
667 : other.m_has_value = true;
668 : }
669 : }
670 : };
671 :
672 : // Copy operations.
673 : template<typename T, typename E,
674 : bool is_trivial = trait_2<std::is_trivially_copy_constructible,
675 : T, E>::value>
676 : struct storage_copy_cons;
677 :
678 : template<typename T, typename E>
679 : struct storage_copy_cons<T, E, true> : storage_1<T, E>
680 : {
681 0 : using storage_1<T, E>::storage_1;
682 : storage_copy_cons (storage_copy_cons &&) = default;
683 : storage_copy_cons &
684 : operator= (const storage_copy_cons& other) = default;
685 : storage_copy_cons &
686 : operator= (storage_copy_cons&& other) = default;
687 :
688 : // Does not require explicit copy constructor.
689 : storage_copy_cons (const storage_copy_cons &) = default;
690 : };
691 :
692 : template<typename T, typename E>
693 : struct storage_copy_cons<T, E, false> : storage_1<T, E>
694 : {
695 : using storage_1<T, E>::storage_1;
696 : storage_copy_cons (storage_copy_cons &&) = default;
697 : storage_copy_cons &
698 : operator= (const storage_copy_cons& other) = default;
699 : storage_copy_cons &
700 : operator= (storage_copy_cons&& other) = default;
701 :
702 : // Requires explicit copy constructor.
703 : constexpr
704 : storage_copy_cons (const storage_copy_cons &o)
705 : : storage_1<T, E>(in_unused)
706 : { this->construct (o); }
707 : };
708 :
709 : template<typename T, typename E,
710 : bool is_trivial = and_<
711 : std::is_trivially_copy_constructible<T>,
712 : std::is_trivially_copy_assignable<T>,
713 : std::is_trivially_destructible<T>,
714 : std::is_trivially_copy_constructible<E>,
715 : std::is_trivially_copy_assignable<E>,
716 : std::is_trivially_destructible<E>
717 : >::value>
718 : struct storage_copy_assign;
719 :
720 : template<typename T, typename E>
721 : struct storage_copy_assign<T, E, true> : storage_copy_cons<T, E>
722 : {
723 0 : using storage_copy_cons<T, E>::storage_copy_cons;
724 : storage_copy_assign (const storage_copy_assign &) = default;
725 : storage_copy_assign (storage_copy_assign &&) = default;
726 : storage_copy_assign &
727 : operator= (storage_copy_assign&& other) = default;
728 :
729 : // Trivially copy-assignable.
730 : storage_copy_assign &
731 : operator= (const storage_copy_assign& other)
732 : noexcept (and_<trait_2<std::is_nothrow_copy_assignable, T, E>,
733 : trait_2<std::is_nothrow_copy_constructible, T, E>>::value)
734 : = default;
735 : };
736 :
737 : template<typename T, typename E>
738 : struct storage_copy_assign<T, E, false> : storage_copy_cons<T, E>
739 : {
740 : using storage_copy_cons<T, E>::storage_copy_cons;
741 : storage_copy_assign (const storage_copy_assign &) = default;
742 : storage_copy_assign (storage_copy_assign &&) = default;
743 : storage_copy_assign &
744 : operator= (storage_copy_assign&& other) = default;
745 :
746 : // Requires explicit copy assignment.
747 : GCC_BACKPORT20_CONSTEXPR storage_copy_assign &
748 : operator= (const storage_copy_assign& other)
749 : noexcept (and_<trait_2<std::is_nothrow_copy_assignable, T, E>,
750 : trait_2<std::is_nothrow_copy_constructible, T, E>>::value)
751 : {
752 : this->assign (other);
753 : return *this;
754 : }
755 : };
756 :
757 : // Move operations.
758 : template<typename T, typename E,
759 : bool is_trivial = trait_2<std::is_trivially_move_constructible,
760 : T, E>::value>
761 : struct storage_move_cons;
762 :
763 : template<typename T, typename E>
764 : struct storage_move_cons<T, E, true> : storage_copy_assign<T, E>
765 : {
766 0 : using storage_copy_assign<T, E>::storage_copy_assign;
767 : storage_move_cons (const storage_move_cons &) = default;
768 : storage_move_cons &
769 : operator= (const storage_move_cons &) = default;
770 : storage_move_cons &
771 : operator= (storage_move_cons &&) = default;
772 :
773 : // Does not require explicit move constructor.
774 : storage_move_cons (storage_move_cons &&) = default;
775 : };
776 :
777 : template<typename T, typename E>
778 : struct storage_move_cons<T, E, false> : storage_copy_assign<T, E>
779 : {
780 : using storage_copy_assign<T, E>::storage_copy_assign;
781 : storage_move_cons (const storage_move_cons &) = default;
782 : storage_move_cons &operator= (const storage_move_cons &) = default;
783 : storage_move_cons &operator= (storage_move_cons &&) = default;
784 :
785 : // Requires explicit move constructor.
786 : constexpr
787 : storage_move_cons (storage_move_cons &&o)
788 : noexcept (trait_2<std::is_nothrow_move_constructible, T, E>::value)
789 : : storage_copy_assign<T, E>(in_unused)
790 : { this->construct (std::move (o)); }
791 : };
792 :
793 : template<typename T, typename E,
794 : bool is_trivial = and_<
795 : trait_2<std::is_trivially_move_constructible, T, E>,
796 : trait_2<std::is_trivially_move_assignable, T, E>,
797 : trait_2<std::is_trivially_destructible, T, E>
798 : >::value>
799 : struct storage_move_assign;
800 :
801 : template<typename T, typename E>
802 : struct storage_move_assign<T, E, true> : storage_move_cons<T, E>
803 : {
804 0 : using storage_move_cons<T, E>::storage_move_cons;
805 : storage_move_assign (storage_move_assign&&) = default;
806 : storage_move_assign (const storage_move_assign&) = default;
807 : storage_move_assign&
808 : operator= (const storage_move_assign&) = default;
809 :
810 : // Trivially move-assignable.
811 : storage_move_assign &
812 : operator= (storage_move_assign&& other)
813 : noexcept (and_<trait_2<std::is_nothrow_move_assignable, T, E>,
814 : trait_2<std::is_nothrow_move_constructible, T, E>>::value)
815 : = default;
816 : };
817 :
818 : template<typename T, typename E>
819 : struct storage_move_assign<T, E, false> : storage_move_cons<T, E>
820 : {
821 : using storage_move_cons<T, E>::storage_move_cons;
822 : storage_move_assign (storage_move_assign&&) = default;
823 : storage_move_assign (const storage_move_assign&) = default;
824 : storage_move_assign&
825 : operator= (const storage_move_assign&) = default;
826 :
827 : // Requires explicit move assignment.
828 : GCC_BACKPORT20_CONSTEXPR storage_move_assign &
829 : operator= (storage_move_assign&& other)
830 : noexcept (and_<trait_2<std::is_nothrow_move_assignable, T, E>,
831 : trait_2<std::is_nothrow_move_constructible, T, E>>::value)
832 : {
833 : this->assign (std::move (other));
834 : return *this;
835 : }
836 : };
837 :
838 : // Default construction.
839 : template<typename T, typename E,
840 : bool is_default_cons = std::is_default_constructible<T>::value>
841 : struct storage_default_cons;
842 :
843 : template<typename T, typename E>
844 : struct storage_default_cons<T, E, false> : storage_move_assign<T, E>
845 : {
846 : using storage_move_assign<T, E>::storage_move_assign;
847 : // No default constructor.
848 : constexpr storage_default_cons () = delete;
849 : };
850 :
851 : template<typename T, typename E>
852 : struct storage_default_cons<T, E, true> : storage_move_assign<T, E>
853 : {
854 0 : using storage_move_assign<T, E>::storage_move_assign;
855 : constexpr storage_default_cons ()
856 : noexcept (std::is_nothrow_constructible<T>::value)
857 : : storage_move_assign<T, E> (in_value)
858 : {}
859 : };
860 :
861 : // Final link in the chain.
862 : template<typename T, typename E>
863 : using storage = storage_default_cons<T, E>;
864 : }} // namespace detail::exp
865 :
866 : // [expected.expected]
867 : template<typename T, typename E, bool is_void>
868 : struct [[nodiscard]] expected
869 : : private detail::exp::storage<T, E>
870 : , private G_Enable_copy_move<
871 : // Copy constructor.
872 : detail::exp::trait_2<std::is_copy_constructible, T, E>::value,
873 : // Copy assignment.
874 : and_<detail::exp::trait_2<std::is_copy_assignable, T, E>,
875 : detail::exp::trait_2<std::is_copy_constructible, T, E>,
876 : or_<std::is_nothrow_move_constructible<T>,
877 : std::is_nothrow_move_constructible<E>>
878 : >::value,
879 : // Move constructor.
880 : detail::exp::trait_2<std::is_move_constructible, T, E>::value,
881 : // Move assignment.
882 : and_<detail::exp::trait_2<std::is_move_assignable, T, E>,
883 : detail::exp::trait_2<std::is_move_constructible, T, E>,
884 : or_<std::is_nothrow_move_constructible<T>,
885 : std::is_nothrow_move_constructible<E>>>::value
886 : >
887 : {
888 : private:
889 : using storage = detail::exp::storage<T, E>;
890 :
891 : // If T is not cv bool, converts-from-any-cvref<T, expected<U, G>> and
892 : // is_constructible<unexpected<E>, cv expected<U, G> ref-qual> are false.
893 : template<typename G_Up, typename G_Gr, typename G_Unex = unexpected<E>,
894 : typename = std::remove_cv_t<T>>
895 : struct g_cons_from_expected
896 : : or_<std::is_constructible<T, expected<G_Up, G_Gr>&>,
897 : std::is_constructible<T, expected<G_Up, G_Gr>>,
898 : std::is_constructible<T, const expected<G_Up, G_Gr>&>,
899 : std::is_constructible<T, const expected<G_Up, G_Gr>>,
900 : std::is_convertible<expected<G_Up, G_Gr>&, T>,
901 : std::is_convertible<expected<G_Up, G_Gr>, T>,
902 : std::is_convertible<const expected<G_Up, G_Gr>&, T>,
903 : std::is_convertible<const expected<G_Up, G_Gr>, T>,
904 : std::is_constructible<G_Unex, expected<G_Up, G_Gr>&>,
905 : std::is_constructible<G_Unex, expected<G_Up, G_Gr>>,
906 : std::is_constructible<G_Unex, const expected<G_Up, G_Gr>&>,
907 : std::is_constructible<G_Unex, const expected<G_Up, G_Gr>>
908 : >
909 : {};
910 :
911 : // _GLIBCXX_RESOLVE_LIB_DEFECTS
912 : // If t is cv bool, we know it can be constructed from expected<U, G>,
913 : // but we don't want to cause the expected (U&&) constructor to be used,
914 : // so we only check the is_constructible<unexpected<E>, ...> cases.
915 : template<typename G_Up, typename G_Gr, typename G_Unex>
916 : struct g_cons_from_expected<G_Up, G_Gr, G_Unex, bool>
917 : : or_<std::is_constructible<G_Unex, expected<G_Up, G_Gr>&>,
918 : std::is_constructible<G_Unex, expected<G_Up, G_Gr>>,
919 : std::is_constructible<G_Unex, const expected<G_Up, G_Gr>&>,
920 : std::is_constructible<G_Unex, const expected<G_Up, G_Gr>>
921 : >
922 : {};
923 :
924 : template<typename G_Up, typename G_Gr>
925 : using g_explicit_conv
926 : = or_<not_<std::is_convertible<G_Up, T>>,
927 : not_<std::is_convertible<G_Gr, E>>
928 : >;
929 :
930 : // Constraints for 'swap'
931 : template<typename T_, typename E_>
932 : using Constrain_swap = std::enable_if_t<and_<
933 : g_is_swappable<T_>,
934 : g_is_swappable<E_>,
935 : std::is_move_constructible<T_>,
936 : std::is_move_constructible<E_>,
937 : or_<
938 : std::is_nothrow_move_constructible<T_>,
939 : std::is_nothrow_move_constructible<E_>
940 : >
941 : >::value>;
942 :
943 : public:
944 : static_assert (detail::exp::can_be_unexpected_val<E>::value,
945 : "E not a valid expected error type");
946 : static_assert (detail::exp::can_be_expected_val<T>::value,
947 : "T cannot be expected<>");
948 :
949 : using value_type = T;
950 : using error_type = E;
951 : using unexpected_type = unexpected<E>;
952 :
953 : template<typename U>
954 : using rebind = expected<U, error_type>;
955 :
956 : // [expected.object.obs]
957 :
958 : // [expected.object.obs]p1
959 : [[nodiscard]] constexpr const T*
960 : operator-> () const noexcept
961 : {
962 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
963 : return std::addressof (this->m_value);
964 : }
965 :
966 : [[nodiscard]] constexpr T*
967 : operator-> () noexcept
968 : {
969 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
970 : return std::addressof (this->m_value);
971 : }
972 :
973 : // [expected.object.obs]p3
974 : [[nodiscard]] constexpr const T&
975 : operator* () const & noexcept
976 : {
977 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
978 : return this->m_value;
979 : }
980 :
981 : [[nodiscard]] constexpr T&
982 : operator* () & noexcept
983 : {
984 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
985 : return this->m_value;
986 : }
987 :
988 : // [expected.object.obs]p5
989 : [[nodiscard]] constexpr T&&
990 : operator* () && noexcept
991 : {
992 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
993 : return std::move (this->m_value);
994 : }
995 :
996 : [[nodiscard]] constexpr const T&&
997 : operator* () const && noexcept
998 : {
999 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
1000 : return std::move (this->m_value);
1001 : }
1002 :
1003 : // [expected.object.obs]p7
1004 : [[nodiscard]] constexpr explicit
1005 : operator bool () const noexcept
1006 : { return this->m_has_value; }
1007 :
1008 : [[nodiscard]] constexpr bool
1009 : has_value () const noexcept
1010 : { return this->m_has_value; }
1011 :
1012 : // [expected.object.obs]p8
1013 : [[nodiscard]] constexpr bool
1014 : has_error () const noexcept
1015 : { return !this->m_has_value; }
1016 :
1017 : // TODO(arsen): restore [[likely]]
1018 :
1019 : // [expected.object.obs]p9
1020 : constexpr const T&
1021 : value () const &
1022 : {
1023 : static_assert (std::is_copy_constructible<E>::value, "");
1024 : if (this->m_has_value)
1025 : return this->m_value;
1026 : g_throw_or_abort (bad_expected_access<E> (gcc::as_const (error ())));
1027 : }
1028 :
1029 : constexpr T&
1030 : value () &
1031 : {
1032 : static_assert (std::is_copy_constructible<E>::value, "");
1033 : if (this->m_has_value)
1034 : return this->m_value;
1035 : g_throw_or_abort (bad_expected_access<E> (gcc::as_const (error ())));
1036 : }
1037 :
1038 : // [expected.object.obs]p12
1039 : constexpr const T&&
1040 : value () const &&
1041 : {
1042 : static_assert (std::is_copy_constructible<E>::value, "");
1043 : static_assert (std::is_constructible<E, const E&&>::value, "");
1044 : if (this->m_has_value)
1045 : return std::move (this->m_value);
1046 : g_throw_or_abort (bad_expected_access<E> (std::move (error ())));
1047 : }
1048 :
1049 : constexpr T&&
1050 : value () &&
1051 : {
1052 : static_assert (std::is_copy_constructible<E>::value, "");
1053 : static_assert (std::is_constructible<E, E&&>::value, "");
1054 : if (this->m_has_value)
1055 : return std::move (this->m_value);
1056 : g_throw_or_abort (bad_expected_access<E> (std::move (error ())));
1057 : }
1058 :
1059 : // [expected.object.obs]p15
1060 : constexpr const E&
1061 : error () const & noexcept
1062 : {
1063 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1064 : return this->m_unex;
1065 : }
1066 :
1067 : constexpr E&
1068 : error () & noexcept
1069 : {
1070 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1071 : return this->m_unex;
1072 : }
1073 :
1074 : // [expected.object.obs]p17
1075 : constexpr const E&&
1076 : error () const && noexcept
1077 : {
1078 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1079 : return std::move (this->m_unex);
1080 : }
1081 :
1082 : constexpr E&&
1083 : error () && noexcept
1084 : {
1085 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1086 : return std::move (this->m_unex);
1087 : }
1088 :
1089 : // [expected.object.obs]p19
1090 : template<typename U = std::remove_cv_t<T>>
1091 : constexpr T
1092 : value_or (U &&v) const &
1093 : {
1094 : static_assert (std::is_convertible<U, T>::value,
1095 : "lvalue-reference value_or requires that type of "
1096 : "argument is convertible to the value type");
1097 : static_assert (std::is_copy_constructible<T>::value,
1098 : "lvalue-reference value_or requires that value type is "
1099 : "copy-constructible");
1100 : return (this->m_has_value
1101 : ? **this
1102 : : static_cast<T> (std::forward<U> (v)));
1103 : }
1104 :
1105 : // [expected.object.obs]p21
1106 : template<typename U = std::remove_cv_t<T>>
1107 : constexpr T
1108 : value_or (U &&v) &&
1109 : {
1110 : static_assert (std::is_convertible<U, T>::value,
1111 : "rvalue-reference value_or requires that type of "
1112 : "argument is convertible to the value type");
1113 : static_assert (std::is_move_constructible<T>::value,
1114 : "rvalue-reference value_or requires that value type is "
1115 : "move-constructible");
1116 : return (this->m_has_value
1117 : ? std::move (**this)
1118 : : static_cast<T> (std::forward<U> (v)));
1119 : }
1120 :
1121 : // [expected.object.obs]p23
1122 : template<typename G = E>
1123 : constexpr E
1124 : error_or (G &&e) const &
1125 : {
1126 : static_assert (std::is_convertible<G, E>::value,
1127 : "lvalue-reference error_or requires that type of "
1128 : "argument is convertible to the value type");
1129 : static_assert (std::is_copy_constructible<E>::value,
1130 : "lvalue-reference error_or requires that error type is "
1131 : "copy-constructible");
1132 : if (this->m_has_value)
1133 : return std::forward<G> (e);
1134 : return this->m_unex;
1135 : }
1136 :
1137 : // [expected.object.obs]p26
1138 : template<typename G = E>
1139 : constexpr E
1140 : error_or (G &&e) &&
1141 : {
1142 : static_assert (std::is_convertible<G, E>::value,
1143 : "rvalue-reference error_or requires that type of "
1144 : "argument is convertible to the value type");
1145 : static_assert (std::is_move_constructible<E>::value,
1146 : "rvalue-reference error_or requires that error type is "
1147 : "move-constructible");
1148 : if (this->m_has_value)
1149 : return std::forward<G> (e);
1150 : return std::move (this->m_unex);
1151 : }
1152 :
1153 : // Constructors [expected.object.cons]
1154 :
1155 : // [expected.object.cons]p1; condition provided by base class
1156 : expected () = default;
1157 :
1158 : // [expected.object.cons]p6 and [expected.object.cons]p11 - the copy/move
1159 : // constructors - are implemented by storage and constrained by
1160 : // G_Enable_copy_move.
1161 : expected (const expected&) = default;
1162 : expected (expected&&) = default;
1163 :
1164 : // [expected.object.cons]p17
1165 : template<typename U, typename G,
1166 : G_Requires<
1167 : // Constraints:
1168 : not_<g_cons_from_expected<U, G>>,
1169 : std::is_constructible<T, const U&>,
1170 : std::is_constructible<E, const G&>,
1171 :
1172 : // !Explicit:
1173 : not_<g_explicit_conv<const U&, const G&>>
1174 : > = false>
1175 : constexpr expected (const expected<U, G>& rhs)
1176 : : storage (detail::exp::in_unused)
1177 : {
1178 : if (rhs.has_value ())
1179 : this->construct_value (*rhs);
1180 : else
1181 : this->construct_unex (rhs.error ());
1182 : }
1183 :
1184 : template<typename U, typename G,
1185 : G_Requires<
1186 : // Constraints:
1187 : not_<g_cons_from_expected<U, G>>,
1188 : std::is_constructible<T, const U&>,
1189 : std::is_constructible<E, const G&>,
1190 :
1191 : // Explicit:
1192 : g_explicit_conv<const U&, const G&>
1193 : > = false>
1194 : constexpr explicit expected (const expected<U, G>& rhs)
1195 : : storage (detail::exp::in_unused)
1196 : {
1197 : if (rhs.has_value ())
1198 : this->construct_value (*rhs);
1199 : else
1200 : this->construct_unex (rhs.error ());
1201 : }
1202 :
1203 : template<typename U, typename G,
1204 : G_Requires<
1205 : // Constraints:
1206 : not_<g_cons_from_expected<U, G>>,
1207 : std::is_constructible<T, U&&>,
1208 : std::is_constructible<E, G&&>,
1209 :
1210 : // !Explicit:
1211 : not_<g_explicit_conv<U, G>>
1212 : > = false>
1213 : constexpr expected (expected<U, G>&& rhs)
1214 : : storage (detail::exp::in_unused)
1215 : {
1216 : if (rhs.has_value ())
1217 : this->construct_value (std::move (*rhs));
1218 : else
1219 : this->construct_unex (std::move (rhs.m_unex));
1220 : }
1221 :
1222 : template<typename U, typename G,
1223 : G_Requires<
1224 : // Constraints:
1225 : not_<g_cons_from_expected<U, G>>,
1226 : std::is_constructible<T, U&&>,
1227 : std::is_constructible<E, G&&>,
1228 :
1229 : // Explicit:
1230 : g_explicit_conv<U, G>
1231 : > = false>
1232 : constexpr explicit expected (expected<U, G>&& rhs)
1233 : : storage (detail::exp::in_unused)
1234 : {
1235 : if (rhs.has_value ())
1236 : this->construct_value (std::move (*rhs));
1237 : else
1238 : this->construct_unex (std::move (rhs.m_unex));
1239 : }
1240 :
1241 : // [expected.object.cons]p23
1242 : template<typename U = std::remove_cv_t<T>,
1243 : G_Requires<
1244 : // Constraints:
1245 : not_<std::is_same<remove_cvref_t<U>, in_place_t>>,
1246 : not_<std::is_same<remove_cvref_t<U>, expected>>,
1247 : not_<std::is_same<remove_cvref_t<U>, unexpect_t>>,
1248 : not_<detail::exp::is_unexpected<remove_cvref_t<U>>>,
1249 : std::is_constructible<T, U>,
1250 : // "if T is cv bool, remove_cvref_t<U> is not a specialization
1251 : // of expected"
1252 : or_<not_<std::is_same<std::remove_cv_t<T>, bool>>,
1253 : not_<detail::exp::is_expected<remove_cvref_t<U>>>>,
1254 :
1255 : // !Explicit:
1256 : std::is_convertible<U, T>
1257 : > = false>
1258 0 : constexpr expected (U&& v)
1259 0 : : storage (detail::exp::in_value, std::forward<U> (v))
1260 : {}
1261 :
1262 : template<typename U = std::remove_cv_t<T>,
1263 : G_Requires<
1264 : // Constraints:
1265 : not_<std::is_same<remove_cvref_t<U>, in_place_t>>,
1266 : not_<std::is_same<remove_cvref_t<U>, expected>>,
1267 : not_<std::is_same<remove_cvref_t<U>, unexpect_t>>,
1268 : not_<detail::exp::is_unexpected<remove_cvref_t<U>>>,
1269 : std::is_constructible<T, U>,
1270 : // "if T is cv bool, remove_cvref_t<U> is not a specialization
1271 : // of expected"
1272 : or_<not_<std::is_same<std::remove_cv_t<T>, bool>>,
1273 : not_<detail::exp::is_expected<remove_cvref_t<U>>>>,
1274 :
1275 : // Explicit:
1276 : not_<std::is_convertible<U, T>>
1277 : > = false>
1278 : constexpr explicit expected (U&& v)
1279 : : storage (detail::exp::in_value, std::forward<U> (v))
1280 : {}
1281 :
1282 : // [expected.object.cons]p27
1283 : template<typename G,
1284 : G_Requires<
1285 : // Constraints:
1286 : std::is_constructible<E, const G&>,
1287 :
1288 : // !Explicit:
1289 : std::is_convertible<const G&, E>
1290 : > = false>
1291 : constexpr expected (const unexpected<G> &e)
1292 : : storage (detail::exp::in_unex, std::forward<const G&> (e.error ()))
1293 : {}
1294 :
1295 : template<typename G,
1296 : G_Requires<
1297 : // Constraints:
1298 : std::is_constructible<E, const G&>,
1299 :
1300 : // Explicit:
1301 : not_<std::is_convertible<const G&, E>>
1302 : > = false>
1303 : constexpr explicit expected (const unexpected<G> &e)
1304 : : storage (detail::exp::in_unex, std::forward<const G&> (e.error ()))
1305 : {}
1306 :
1307 : template<typename G,
1308 : G_Requires<
1309 : // Constraints:
1310 : std::is_constructible<E, G&&>,
1311 :
1312 : // !Explicit:
1313 : std::is_convertible<G&&, E>
1314 : > = false>
1315 : constexpr expected (unexpected<G> &&e)
1316 : : storage (detail::exp::in_unex, std::forward<G&&> (e.error ()))
1317 : {}
1318 :
1319 : template<typename G,
1320 : G_Requires<
1321 : // Constraints:
1322 : std::is_constructible<E, G&&>,
1323 :
1324 : // Explicit:
1325 : not_<std::is_convertible<G&&, E>>
1326 : > = false>
1327 : constexpr explicit expected (unexpected<G> &&e)
1328 : : storage (detail::exp::in_unex, std::forward<G&&> (e.error ()))
1329 : {}
1330 :
1331 : // [expected.object.cons]p32
1332 : template<typename... Args,
1333 : G_Requires<
1334 : std::is_constructible<T, Args...>
1335 : > = false>
1336 : constexpr explicit expected (in_place_t, Args&&... args)
1337 : : storage (detail::exp::in_value, std::forward<Args> (args)...)
1338 : {}
1339 :
1340 : // [expected.object.cons]p36
1341 : template<typename U, typename... Args,
1342 : G_Requires<
1343 : std::is_constructible<T,
1344 : std::initializer_list<U>&, Args...>
1345 : > = false>
1346 : constexpr explicit expected (in_place_t, std::initializer_list<U> il,
1347 : Args&&... args)
1348 : : storage (detail::exp::in_value, il, std::forward<Args> (args)...)
1349 : {}
1350 :
1351 : // [expected.object.cons]p40
1352 : template<typename... Args,
1353 : G_Requires<
1354 : std::is_constructible<E, Args...>
1355 : > = false>
1356 : constexpr explicit expected (unexpect_t, Args&&... args)
1357 : : storage (detail::exp::in_unex, std::forward<Args> (args)...)
1358 : {}
1359 :
1360 : // [expected.object.cons]p44
1361 : template<typename U, typename... Args,
1362 : G_Requires<
1363 : std::is_constructible<E, std::initializer_list<U>&, Args...>
1364 : > = false>
1365 : constexpr explicit expected (unexpect_t, std::initializer_list<U> il,
1366 : Args&&... args)
1367 : : storage (detail::exp::in_unex, il, std::forward<Args> (args)...)
1368 : {}
1369 :
1370 : // [expected.object.dtor], implemented in storage
1371 : ~expected () = default;
1372 :
1373 : // [expected.object.assign]
1374 : // The move/copy assignment operators are in the base class, and
1375 : // constrained by G_Enable_copy_move, much like the constructors.
1376 : expected &
1377 : operator= (const expected &) = default;
1378 : expected &
1379 : operator= (expected &&) = default;
1380 :
1381 : // [expected.object.assign]p11
1382 : template<typename U = std::remove_cv_t<T>,
1383 : G_Requires<
1384 : not_<std::is_same<expected, remove_cvref_t<U>>>,
1385 : not_<detail::exp::is_unexpected<remove_cvref_t<U>>>,
1386 : std::is_constructible<T, U>,
1387 : std::is_assignable<T&, U>,
1388 :
1389 : or_<std::is_nothrow_constructible<T, U>,
1390 : std::is_nothrow_move_constructible<T>,
1391 : std::is_nothrow_move_constructible<E>>
1392 : > = false>
1393 : GCC_BACKPORT20_CONSTEXPR expected& operator= (U&& v)
1394 : {
1395 : this->assign_value (std::forward<U> (v));
1396 : return *this;
1397 : }
1398 :
1399 : // [expected.object.assign]p14
1400 : template<typename G,
1401 : G_Requires<
1402 : std::is_constructible<E, const G &>,
1403 : std::is_assignable<E&, const G &>,
1404 :
1405 : or_<std::is_nothrow_constructible<E, const G &>,
1406 : std::is_nothrow_move_constructible<T>,
1407 : std::is_nothrow_move_constructible<E>>
1408 : > = false>
1409 : GCC_BACKPORT20_CONSTEXPR expected &
1410 : operator= (const unexpected<G>& e)
1411 : {
1412 : this->assign_unex (std::forward<const G &> (e.error ()));
1413 : return *this;
1414 : }
1415 :
1416 : template<typename G,
1417 : G_Requires<
1418 : std::is_constructible<E, G &&>,
1419 : std::is_assignable<E&, G &&>,
1420 :
1421 : or_<std::is_nothrow_constructible<E, G &&>,
1422 : std::is_nothrow_move_constructible<T>,
1423 : std::is_nothrow_move_constructible<E>>
1424 : > = false>
1425 : GCC_BACKPORT20_CONSTEXPR expected &
1426 : operator= (unexpected<G>&& e)
1427 : {
1428 : this->assign_unex (std::forward<G &&> (e.error ()));
1429 : return *this;
1430 : }
1431 :
1432 : // [expected.object.assign]p18
1433 : template<typename... Args,
1434 : G_Requires<
1435 : std::is_nothrow_constructible<T, Args...>
1436 : > = false>
1437 : constexpr T &
1438 : emplace (Args&&... args) noexcept
1439 : {
1440 : this->clear_storage ();
1441 : this->m_has_value = true;
1442 : return *gcc::construct_at (std::addressof (this->m_value),
1443 : std::forward<Args> (args)...);
1444 : }
1445 :
1446 : // [expected.object.assign]p20
1447 : template<typename U, typename... Args,
1448 : G_Requires<
1449 : std::is_nothrow_constructible<T,
1450 : std::initializer_list<U>&,
1451 : Args...>
1452 : > = false>
1453 : constexpr T &
1454 : emplace (std::initializer_list<U> il, Args&&... args) noexcept
1455 : {
1456 : this->clear_storage ();
1457 : this->m_has_value = true;
1458 : return *gcc::construct_at (std::addressof (this->m_value),
1459 : il, std::forward<Args> (args)...);
1460 : }
1461 :
1462 : // [expected.object.swap]
1463 : template<typename T_ = T, typename E_ = E>
1464 : constexpr Constrain_swap<T_, E_>
1465 : swap (expected& rhs)
1466 : noexcept (and_<std::is_nothrow_move_constructible<T>,
1467 : g_is_nothrow_swappable<T>,
1468 : std::is_nothrow_move_constructible<E>,
1469 : g_is_nothrow_swappable<E>>::value)
1470 : { this->_swap (static_cast<storage &> (rhs)); }
1471 :
1472 : template<typename T_ = T, typename E_ = E>
1473 : friend constexpr
1474 : Constrain_swap<T_, E_>
1475 : swap (expected& x, expected& y) noexcept (noexcept (x.swap (y)))
1476 : { x.swap (y); }
1477 :
1478 : // [expected.object.monadic]
1479 :
1480 : // [expected.object.monadic]p1
1481 : template<typename F,
1482 : typename E_ = E,
1483 : G_Requires<
1484 : std::is_constructible<E_, E_ &>
1485 : > = false>
1486 : constexpr auto
1487 : and_then (F &&f) &
1488 : {
1489 : using U = remove_cvref_t<g_invoke_result_t<F, decltype (**this)>>;
1490 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
1491 : "the function passed to std::expected<T, E>::and_then "
1492 : "must return a std::expected with same error type");
1493 : if (has_value ())
1494 : return gcc::invoke (std::forward<F> (f), **this);
1495 : else
1496 : return U (unexpect, this->m_unex);
1497 : }
1498 :
1499 : template<typename F,
1500 : typename E_ = E,
1501 : G_Requires<
1502 : std::is_constructible<E_, const E_ &>
1503 : > = false>
1504 : constexpr auto
1505 : and_then (F &&f) const &
1506 : {
1507 : using U = remove_cvref_t<g_invoke_result_t<F, decltype (**this)>>;
1508 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
1509 : "the function passed to std::expected<T, E>::and_then "
1510 : "must return a std::expected with same error type");
1511 : if (has_value ())
1512 : return gcc::invoke (std::forward<F> (f), **this);
1513 : else
1514 : return U (unexpect, this->m_unex);
1515 : }
1516 :
1517 : // [expected.object.monadic]p5
1518 : template<typename F,
1519 : typename E_ = E,
1520 : G_Requires<
1521 : std::is_constructible<E_, E_ &&>
1522 : > = false>
1523 : constexpr auto
1524 : and_then (F &&f) &&
1525 : {
1526 : using U = remove_cvref_t<g_invoke_result_t<F, decltype (std::move (**this))>>;
1527 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
1528 : "the function passed to std::expected<T, E>::and_then "
1529 : "must return a std::expected with same error type");
1530 : if (has_value ())
1531 : return gcc::invoke (std::forward<F> (f), std::move (**this));
1532 : else
1533 : return U (unexpect, std::move (this->m_unex));
1534 : }
1535 :
1536 : template<typename F,
1537 : typename E_ = E,
1538 : G_Requires<
1539 : std::is_constructible<E_, const E_ &&>
1540 : > = false>
1541 : constexpr auto
1542 : and_then (F &&f) const &&
1543 : {
1544 : using U = remove_cvref_t<g_invoke_result_t<F, decltype (std::move (**this))>>;
1545 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
1546 : "the function passed to std::expected<T, E>::and_then "
1547 : "must return a std::expected with same error type");
1548 : if (has_value ())
1549 : return gcc::invoke (std::forward<F> (f), std::move (**this));
1550 : else
1551 : return U (unexpect, std::move (this->m_unex));
1552 : }
1553 :
1554 : // [expected.object.monadic]p9
1555 : template<typename F,
1556 : typename T_ = T,
1557 : G_Requires<
1558 : std::is_constructible<T_, T_ &>
1559 : > = false>
1560 : constexpr auto
1561 : or_else (F &&f) &
1562 : {
1563 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (error ())>>;
1564 : static_assert (detail::exp::is_expected_with_value<G, T>::value,
1565 : "the function passed to std::expected<T, E>::or_else "
1566 : "must return a std::expected with same value type");
1567 : if (has_value ())
1568 : return G (in_place, **this);
1569 : else
1570 : return gcc::invoke (std::forward<F> (f), this->m_unex);
1571 : }
1572 :
1573 : template<typename F,
1574 : typename T_ = T,
1575 : G_Requires<
1576 : std::is_constructible<T_, const T_&>
1577 : > = false>
1578 : constexpr auto
1579 : or_else (F &&f) const &
1580 : {
1581 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (error ())>>;
1582 : static_assert (detail::exp::is_expected_with_value<G, T>::value,
1583 : "the function passed to std::expected<T, E>::or_else "
1584 : "must return a std::expected with same value type");
1585 : if (has_value ())
1586 : return G (in_place, **this);
1587 : else
1588 : return gcc::invoke (std::forward<F> (f), this->m_unex);
1589 : }
1590 :
1591 : // [expected.object.monadic]p13
1592 : template<typename F,
1593 : typename T_ = T,
1594 : G_Requires<
1595 : std::is_constructible<T_, T_ &&>
1596 : > = false>
1597 : constexpr auto
1598 : or_else (F &&f) &&
1599 : {
1600 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
1601 : static_assert (detail::exp::is_expected_with_value<G, T>::value,
1602 : "the function passed to std::expected<T, E>::or_else "
1603 : "must return a std::expected with same value type");
1604 : if (has_value ())
1605 : return G (in_place, std::move (**this));
1606 : else
1607 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
1608 : }
1609 :
1610 : template<typename F,
1611 : typename T_ = T,
1612 : G_Requires<
1613 : std::is_constructible<T_, const T_ &&>
1614 : > = false>
1615 : constexpr auto
1616 : or_else (F &&f) const &&
1617 : {
1618 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
1619 : static_assert (detail::exp::is_expected_with_value<G, T>::value,
1620 : "the function passed to std::expected<T, E>::or_else "
1621 : "must return a std::expected with same value type");
1622 : if (has_value ())
1623 : return G (in_place, std::move (**this));
1624 : else
1625 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
1626 : }
1627 :
1628 : // [expected.object.monadic]p17
1629 : template<typename F,
1630 : typename E_ = E,
1631 : G_Requires<
1632 : std::is_constructible<E_, E_ &>
1633 : > = false>
1634 : constexpr auto
1635 : transform (F &&f) &
1636 : {
1637 : using U = std::remove_cv_t<g_invoke_result_t<F, decltype (**this)>>;
1638 : using Res = expected<U, E>;
1639 : if (has_value ())
1640 : return Res (detail::exp::in_place_inv{}, [&] () {
1641 : return gcc::invoke (std::forward<F> (f), **this);
1642 : });
1643 : else
1644 : return Res (unexpect, this->m_unex);
1645 : }
1646 :
1647 : template<typename F,
1648 : typename E_ = E,
1649 : G_Requires<
1650 : std::is_constructible<E_, const E_ &>
1651 : > = false>
1652 : constexpr auto
1653 : transform (F &&f) const &
1654 : {
1655 : using U = std::remove_cv_t<g_invoke_result_t<F, decltype (**this)>>;
1656 : using Res = expected<U, E>;
1657 : if (has_value ())
1658 : return Res (detail::exp::in_place_inv{}, [&] () {
1659 : return gcc::invoke (std::forward<F> (f), **this);
1660 : });
1661 : else
1662 : return Res (unexpect, this->m_unex);
1663 : }
1664 :
1665 : // [expected.object.monadic]p21
1666 : template<typename F,
1667 : typename E_ = E,
1668 : G_Requires<
1669 : std::is_constructible<E_, E_ &&>
1670 : > = false>
1671 : constexpr auto
1672 : transform (F &&f) &&
1673 : {
1674 : using U = std::remove_cv_t<g_invoke_result_t<F, decltype (std::move (**this))>>;
1675 : using Res = expected<U, E>;
1676 : if (has_value ())
1677 : return Res (detail::exp::in_place_inv{}, [&] () {
1678 : return gcc::invoke (std::forward<F> (f), std::move (**this));
1679 : });
1680 : else
1681 : return Res (unexpect, std::move (this->m_unex));
1682 : }
1683 :
1684 : template<typename F,
1685 : typename E_ = E,
1686 : G_Requires<
1687 : std::is_constructible<E_, const E_ &&>
1688 : > = false>
1689 : constexpr auto
1690 : transform (F &&f) const &&
1691 : {
1692 : using U = std::remove_cv_t<g_invoke_result_t<F, decltype (std::move (**this))>>;
1693 : using Res = expected<U, E>;
1694 : if (has_value ())
1695 : return Res (detail::exp::in_place_inv{}, [&] () {
1696 : return gcc::invoke (std::forward<F> (f), std::move (**this));
1697 : });
1698 : else
1699 : return Res (unexpect, std::move (this->m_unex));
1700 : }
1701 :
1702 : // [expected.object.monadic]p25
1703 : template<typename F,
1704 : typename T_ = T,
1705 : G_Requires<
1706 : std::is_constructible<T_, T_ &>
1707 : > = false>
1708 : constexpr auto
1709 : transform_error (F &&f) &
1710 : {
1711 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (error ())>>;
1712 : using Res = expected<T, G>;
1713 : if (has_value ())
1714 : return Res (in_place, **this);
1715 : else
1716 : return Res (detail::exp::unexpect_inv{}, [&] () {
1717 : return gcc::invoke (std::forward<F> (f), this->m_unex);
1718 : });
1719 : }
1720 :
1721 : template<typename F,
1722 : typename T_ = T,
1723 : G_Requires<
1724 : std::is_constructible<T_, const T_ &>
1725 : > = false>
1726 : constexpr auto
1727 : transform_error (F &&f) const &
1728 : {
1729 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (error ())>>;
1730 : using Res = expected<T, G>;
1731 : if (has_value ())
1732 : return Res (in_place, **this);
1733 : else
1734 : return Res (detail::exp::unexpect_inv{}, [&] () {
1735 : return gcc::invoke (std::forward<F> (f), this->m_unex);
1736 : });
1737 : }
1738 :
1739 : // [expected.object.monadic]p29
1740 : template<typename F,
1741 : typename T_ = T,
1742 : G_Requires<
1743 : std::is_constructible<T_, T_ &&>
1744 : > = false>
1745 : constexpr auto
1746 : transform_error (F &&f) &&
1747 : {
1748 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
1749 : using Res = expected<T, G>;
1750 : if (has_value ())
1751 : return Res (in_place, std::move (**this));
1752 : else
1753 : return Res (detail::exp::unexpect_inv{}, [&] () {
1754 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
1755 : });
1756 : }
1757 :
1758 : template<typename F,
1759 : typename T_ = T,
1760 : G_Requires<
1761 : std::is_constructible<T_, const T_ &&>
1762 : > = false>
1763 : constexpr auto
1764 : transform_error (F &&f) const &&
1765 : {
1766 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
1767 : using Res = expected<T, G>;
1768 : if (has_value ())
1769 : return Res (in_place, std::move (**this));
1770 : else
1771 : return Res (detail::exp::unexpect_inv{}, [&] () {
1772 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
1773 : });
1774 : }
1775 :
1776 : // [expected.object.eq]
1777 : template<typename T2, typename E2>
1778 : [[nodiscard]] friend constexpr
1779 : detail::exp::eql_2_return_t<T, T2, E, E2>
1780 : operator== (const expected &x, const expected<T2, E2> &y)
1781 : {
1782 : if (x.m_has_value && y.m_has_value)
1783 : return *x == *y;
1784 : else if (!x.m_has_value && !y.m_has_value)
1785 : return x.m_unex == y.m_unex;
1786 : return false;
1787 : }
1788 :
1789 : template<typename T2,
1790 : G_Requires<
1791 : not_<detail::exp::is_expected<T2>>
1792 : > = false>
1793 : [[nodiscard]] friend constexpr
1794 : eql_return_t<T, T2>
1795 : operator== (const expected &x, const T2 &y)
1796 : {
1797 : if (!x.m_has_value)
1798 : return false;
1799 : return *x == y;
1800 : }
1801 :
1802 : template<typename E2>
1803 : [[nodiscard]] friend constexpr
1804 : eql_return_t<E, E2>
1805 : operator== (const expected &x, const unexpected<E2> &y)
1806 : {
1807 : if (x.m_has_value)
1808 : return false;
1809 : return x.m_unex == y.unex;
1810 : }
1811 :
1812 : private:
1813 : template<typename Fn>
1814 : explicit constexpr
1815 : expected (detail::exp::in_place_inv tag, Fn&& fn)
1816 : : storage (tag, std::forward<Fn> (fn))
1817 : {}
1818 :
1819 : template<typename Fn>
1820 : explicit constexpr
1821 : expected (detail::exp::unexpect_inv tag, Fn&& fn)
1822 : : storage (tag, std::forward<Fn> (fn))
1823 : {}
1824 :
1825 : template<typename, typename, bool>
1826 : friend struct expected;
1827 : };
1828 :
1829 : template<typename Void, typename E>
1830 : struct [[nodiscard]] expected<Void, E, true>
1831 : : private detail::exp::storage<detail::exp::void_storage, E>
1832 : , private G_Enable_copy_move<
1833 : // Copy constructor.
1834 : std::is_copy_constructible<E>::value,
1835 : // Copy assignment.
1836 : and_<std::is_copy_assignable<E>,
1837 : std::is_copy_constructible<E>>::value,
1838 : // Move constructor.
1839 : std::is_move_constructible<E>::value,
1840 : // Move assignment.
1841 : and_<std::is_move_assignable<E>,
1842 : std::is_move_constructible<E>>::value
1843 : >
1844 : {
1845 : private:
1846 : using storage = detail::exp::storage<detail::exp::void_storage, E>;
1847 :
1848 : template<typename G_Up, typename G_Gr, typename G_Unex = unexpected<E>>
1849 : struct g_cons_from_expected
1850 : : or_<std::is_constructible<G_Unex, expected<G_Up, G_Gr>&>,
1851 : std::is_constructible<G_Unex, expected<G_Up, G_Gr>>,
1852 : std::is_constructible<G_Unex, const expected<G_Up, G_Gr>&>,
1853 : std::is_constructible<G_Unex, const expected<G_Up, G_Gr>>
1854 : >
1855 : {};
1856 :
1857 : template<typename E_>
1858 : using Constrain_swap = std::enable_if_t<and_<
1859 : g_is_swappable<E_>,
1860 : std::is_move_constructible<E_>
1861 : >::value>;
1862 :
1863 : public:
1864 : static_assert (detail::exp::can_be_unexpected_val<E>::value,
1865 : "E not a valid expected error type");
1866 :
1867 : using value_type = Void;
1868 : using error_type = E;
1869 : using unexpected_type = unexpected<E>;
1870 :
1871 : template<typename U>
1872 : using rebind = expected<U, error_type>;
1873 :
1874 : // [expected.void.obs]p1
1875 : [[nodiscard]] constexpr explicit
1876 : operator bool () const noexcept
1877 : { return this->m_has_value; }
1878 :
1879 : [[nodiscard]] constexpr bool
1880 : has_value () const noexcept
1881 : { return this->m_has_value; }
1882 :
1883 : // [expected.void.obs]p2
1884 : [[nodiscard]] constexpr bool
1885 : has_error () const noexcept
1886 : { return !this->m_has_value; }
1887 :
1888 : // [expected.void.obs]p3
1889 : constexpr void
1890 : operator* () const noexcept
1891 : {
1892 : gccbackport_assert (this->m_has_value && "Accessing value of expected with no value");
1893 : }
1894 :
1895 : // [expected.void.obs]p4
1896 : constexpr void
1897 : value () const &
1898 : {
1899 : static_assert (std::is_copy_constructible<E>::value, "");
1900 : if (!this->m_has_value)
1901 : g_throw_or_abort (bad_expected_access<E> (error ()));
1902 : }
1903 :
1904 : // [expected.void.obs]p6
1905 : constexpr void
1906 : value () &&
1907 : {
1908 : static_assert (std::is_copy_constructible<E>::value, "");
1909 : static_assert (std::is_constructible<E, E&&>::value, "");
1910 : if (!this->m_has_value)
1911 : g_throw_or_abort (bad_expected_access<E> (std::move (error ())));
1912 : }
1913 :
1914 : // [expected.void.obs]p8
1915 : constexpr const E&
1916 : error () const & noexcept
1917 : {
1918 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1919 : return this->m_unex;
1920 : }
1921 :
1922 : constexpr E&
1923 : error () & noexcept
1924 : {
1925 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1926 : return this->m_unex;
1927 : }
1928 :
1929 : // [expected.void.obs]p10
1930 : constexpr const E&&
1931 : error () const && noexcept
1932 : {
1933 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1934 : return std::move (this->m_unex);
1935 : }
1936 :
1937 : constexpr E&&
1938 : error () && noexcept
1939 : {
1940 : gccbackport_assert (!this->m_has_value && "Accessing error of expected with no error");
1941 : return std::move (this->m_unex);
1942 : }
1943 :
1944 : // [expected.void.obs]p12
1945 : template<typename G = E>
1946 : constexpr E
1947 : error_or (G &&e) const &
1948 : {
1949 : static_assert (std::is_convertible<G, E>::value,
1950 : "lvalue-reference error_or requires that type of "
1951 : "argument is convertible to the error type");
1952 : static_assert (std::is_copy_constructible<E>::value,
1953 : "lvalue-reference error_or requires that error type is "
1954 : "copy-constructible");
1955 : if (this->m_has_value)
1956 : return std::forward<G> (e);
1957 : return this->m_unex;
1958 : }
1959 :
1960 : // [expected.void.obs]p14
1961 : template<typename G = E>
1962 : constexpr E
1963 : error_or (G &&e) &&
1964 : {
1965 : static_assert (std::is_convertible<G, E>::value,
1966 : "rvalue-reference error_or requires that type of "
1967 : "argument is convertible to the error type");
1968 : static_assert (std::is_move_constructible<E>::value,
1969 : "rvalue-reference error_or requires that error type is "
1970 : "move-constructible");
1971 : if (this->m_has_value)
1972 : return std::forward<G> (e);
1973 : return std::move (this->m_unex);
1974 : }
1975 :
1976 : // Constructors [expected.void.cons]
1977 :
1978 : // [expected.void.cons]p1; condition provided by base class
1979 : expected () = default;
1980 :
1981 : // [expected.void.cons]p2 and [expected.void.cons]p7 - the copy/move
1982 : // constructors - are implemented by storage and constrained by
1983 : // G_Enable_copy_move.
1984 : expected (const expected&) = default;
1985 : expected (expected&&) = default;
1986 :
1987 : // [expected.void.cons]p12
1988 : template<typename U, typename G,
1989 : G_Requires<
1990 : // Constraints:
1991 : std::is_void<U>,
1992 : std::is_constructible<E, const G&>,
1993 : not_<g_cons_from_expected<U, G>>,
1994 :
1995 : // !Explicit:
1996 : std::is_convertible<const G&, E>
1997 : > = false>
1998 : constexpr expected (const expected<U, G>& rhs)
1999 : : storage (detail::exp::in_unused)
2000 : {
2001 : if (rhs.has_value ())
2002 : this->construct_value ();
2003 : else
2004 : this->construct_unex (rhs.error ());
2005 : }
2006 :
2007 : template<typename U, typename G,
2008 : G_Requires<
2009 : // Constraints:
2010 : std::is_void<U>,
2011 : std::is_constructible<E, const G&>,
2012 : not_<g_cons_from_expected<U, G>>,
2013 :
2014 : // Explicit:
2015 : not_<std::is_convertible<const G&, E>>
2016 : > = false>
2017 : constexpr explicit expected (const expected<U, G>& rhs)
2018 : : storage (detail::exp::in_unused)
2019 : {
2020 : if (rhs.has_value ())
2021 : this->construct_value ();
2022 : else
2023 : this->construct_unex (rhs.error ());
2024 : }
2025 :
2026 : template<typename U, typename G,
2027 : G_Requires<
2028 : // Constraints:
2029 : std::is_void<U>,
2030 : std::is_constructible<E, G&&>,
2031 : not_<g_cons_from_expected<U, G>>,
2032 :
2033 : // !Explicit:
2034 : std::is_convertible<G, E>
2035 : > = false>
2036 : constexpr expected (expected<U, G>&& rhs)
2037 : : storage (detail::exp::in_unused)
2038 : {
2039 : if (rhs.has_value ())
2040 : this->construct_value ();
2041 : else
2042 : this->construct_unex (std::move (rhs.m_unex));
2043 : }
2044 :
2045 : template<typename U, typename G,
2046 : G_Requires<
2047 : // Constraints:
2048 : std::is_void<U>,
2049 : std::is_constructible<E, G &&>,
2050 : not_<g_cons_from_expected<U, G>>,
2051 :
2052 : // Explicit:
2053 : not_<std::is_convertible<G, E>>
2054 : > = false>
2055 : constexpr explicit expected (expected<U, G>&& rhs)
2056 : : storage (detail::exp::in_unused)
2057 : {
2058 : if (rhs.has_value ())
2059 : this->construct_value ();
2060 : else
2061 : this->construct_unex (std::move (rhs.m_unex));
2062 : }
2063 :
2064 : // [expected.void.cons]p17
2065 : template<typename G,
2066 : G_Requires<
2067 : // Constraints:
2068 : std::is_constructible<E, const G&>,
2069 :
2070 : // !Explicit:
2071 : std::is_convertible<const G&, E>
2072 : > = false>
2073 : constexpr expected (const unexpected<G> &e)
2074 : : storage (detail::exp::in_unex, std::forward<const G&> (e.error ()))
2075 : {}
2076 :
2077 : template<typename G,
2078 : G_Requires<
2079 : // Constraints:
2080 : std::is_constructible<E, const G&>,
2081 :
2082 : // Explicit:
2083 : not_<std::is_convertible<const G&, E>>
2084 : > = false>
2085 : constexpr explicit expected (const unexpected<G> &e)
2086 : : storage (detail::exp::in_unex, std::forward<const G&> (e.error ()))
2087 : {}
2088 :
2089 : template<typename G,
2090 : G_Requires<
2091 : // Constraints:
2092 : std::is_constructible<E, G&&>,
2093 :
2094 : // !Explicit:
2095 : std::is_convertible<G&&, E>
2096 : > = false>
2097 : constexpr expected (unexpected<G> &&e)
2098 : : storage (detail::exp::in_unex, std::forward<G&&> (e.error ()))
2099 : {}
2100 :
2101 : template<typename G,
2102 : G_Requires<
2103 : // Constraints:
2104 : std::is_constructible<E, G&&>,
2105 :
2106 : // Explicit:
2107 : not_<std::is_convertible<G&&, E>>
2108 : > = false>
2109 : constexpr explicit expected (unexpected<G> &&e)
2110 : : storage (detail::exp::in_unex, std::forward<G&&> (e.error ()))
2111 : {}
2112 :
2113 : // [expected.void.cons]p22
2114 : constexpr explicit expected (in_place_t) noexcept
2115 : : storage (detail::exp::in_value)
2116 : {}
2117 :
2118 : // [expected.void.cons]p23
2119 : template<typename... Args,
2120 : G_Requires<
2121 : std::is_constructible<E, Args...>
2122 : > = false>
2123 : constexpr explicit expected (unexpect_t, Args&&... args)
2124 : : storage (detail::exp::in_unex, std::forward<Args> (args)...)
2125 : {}
2126 :
2127 : // [expected.void.cons]p27
2128 : template<typename U, typename... Args,
2129 : G_Requires<
2130 : std::is_constructible<E, std::initializer_list<U>&, Args...>
2131 : > = false>
2132 : constexpr explicit expected (unexpect_t, std::initializer_list<U> il,
2133 : Args&&... args)
2134 : : storage (detail::exp::in_unex, il, std::forward<Args> (args)...)
2135 : {}
2136 :
2137 : // [expected.void.dtor], implemented in storage
2138 : ~expected () = default;
2139 :
2140 : // [expected.void.assign]p11, p5
2141 : // The move/copy assignment operators are in the base class, and
2142 : // constrained by G_Enable_copy_move, much like the constructors.
2143 : expected &
2144 : operator= (const expected &) = default;
2145 : expected &
2146 : operator= (expected &&) = default;
2147 :
2148 : // [expected.void.assign]p10
2149 : template<typename G,
2150 : G_Requires<
2151 : std::is_constructible<E, const G &>,
2152 : std::is_assignable<E&, const G &>
2153 : > = false>
2154 : GCC_BACKPORT20_CONSTEXPR expected &
2155 : operator= (const unexpected<G>& e)
2156 : {
2157 : this->assign_unex (std::forward<const G &> (e.error ()));
2158 : return *this;
2159 : }
2160 :
2161 : template<typename G,
2162 : G_Requires<
2163 : std::is_constructible<E, G &&>,
2164 : std::is_assignable<E&, G &&>
2165 : > = false>
2166 : GCC_BACKPORT20_CONSTEXPR expected &
2167 : operator= (unexpected<G>&& e)
2168 : {
2169 : this->assign_unex (std::forward<G &&> (e.error ()));
2170 : return *this;
2171 : }
2172 :
2173 : // [expected.void.assign]p14
2174 : constexpr void
2175 : emplace () noexcept
2176 : {
2177 : this->clear_storage ();
2178 : this->construct_value ();
2179 : }
2180 :
2181 : // [expected.void.swap]
2182 : // Not using storage::swap as in the case where one expected has a value
2183 : // and the other does not, it makes an extra unneeded move in order to be
2184 : // able to shift around the T value. We don't have a T value here, but we
2185 : // pretend we do for sake of storage.
2186 : template<typename E_ = E>
2187 : constexpr Constrain_swap<E_>
2188 : swap (expected& other)
2189 : noexcept (and_<std::is_nothrow_move_constructible<E>,
2190 : g_is_nothrow_swappable<E>>::value)
2191 : {
2192 : using std::swap;
2193 :
2194 : if (this->m_has_value && other.m_has_value)
2195 : ;
2196 : else if (!this->m_has_value && !other.m_has_value)
2197 : swap (this->m_unex, other.m_unex);
2198 : else if (!this->m_has_value && other.m_has_value)
2199 : other.swap (*this);
2200 : else
2201 : {
2202 : gcc::construct_at (std::addressof (this->m_unex),
2203 : std::move (other.m_unex));
2204 : gcc::destroy_at (std::addressof (other.m_unex));
2205 : this->m_has_value = false;
2206 : other.construct_value ();
2207 : }
2208 : }
2209 :
2210 : template<typename E_ = E>
2211 : friend constexpr
2212 : Constrain_swap<E_>
2213 : swap (expected& x, expected& y) noexcept (noexcept (x.swap (y)))
2214 : { x.swap (y); }
2215 :
2216 : // [expected.void.monadic]p1
2217 : template<typename F,
2218 : typename E_ = E,
2219 : G_Requires<
2220 : std::is_constructible<E_, E_ &>
2221 : > = false>
2222 : constexpr auto
2223 : and_then (F &&f) &
2224 : {
2225 : using U = remove_cvref_t<g_invoke_result_t<F>>;
2226 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
2227 : "the function passed to std::expected<T, E>::and_then "
2228 : "must return a std::expected with same error type");
2229 : if (has_value ())
2230 : return gcc::invoke (std::forward<F> (f));
2231 : else
2232 : return U (unexpect, this->m_unex);
2233 : }
2234 :
2235 : template<typename F,
2236 : typename E_ = E,
2237 : G_Requires<
2238 : std::is_constructible<E_, const E_ &>
2239 : > = false>
2240 : constexpr auto
2241 : and_then (F &&f) const &
2242 : {
2243 : using U = remove_cvref_t<g_invoke_result_t<F>>;
2244 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
2245 : "the function passed to std::expected<T, E>::and_then "
2246 : "must return a std::expected with same error type");
2247 : if (has_value ())
2248 : return gcc::invoke (std::forward<F> (f));
2249 : else
2250 : return U (unexpect, this->m_unex);
2251 : }
2252 :
2253 : // [expected.void.monadic]p5
2254 : template<typename F,
2255 : typename E_ = E,
2256 : G_Requires<
2257 : std::is_constructible<E_, E_ &&>
2258 : > = false>
2259 : constexpr auto
2260 : and_then (F &&f) &&
2261 : {
2262 : using U = remove_cvref_t<g_invoke_result_t<F>>;
2263 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
2264 : "the function passed to std::expected<T, E>::and_then "
2265 : "must return a std::expected with same error type");
2266 : if (has_value ())
2267 : return gcc::invoke (std::forward<F> (f));
2268 : else
2269 : return U (unexpect, std::move (this->m_unex));
2270 : }
2271 :
2272 : template<typename F,
2273 : typename E_ = E,
2274 : G_Requires<
2275 : std::is_constructible<E_, const E_ &&>
2276 : > = false>
2277 : constexpr auto
2278 : and_then (F &&f) const &&
2279 : {
2280 : using U = remove_cvref_t<g_invoke_result_t<F>>;
2281 : static_assert (detail::exp::is_expected_with_error<U, E>::value,
2282 : "the function passed to std::expected<T, E>::and_then "
2283 : "must return a std::expected with same error type");
2284 : if (has_value ())
2285 : return gcc::invoke (std::forward<F> (f));
2286 : else
2287 : return U (unexpect, std::move (this->m_unex));
2288 : }
2289 :
2290 : // [expected.void.monadic]p9
2291 : template<typename F>
2292 : constexpr auto
2293 : or_else (F &&f) &
2294 : {
2295 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (error ())>>;
2296 : static_assert (detail::exp::is_expected_with_value<G, Void>::value,
2297 : "the function passed to std::expected<T, E>::or_else "
2298 : "must return a std::expected with same value type");
2299 : if (has_value ())
2300 : return G ();
2301 : else
2302 : return gcc::invoke (std::forward<F> (f), this->m_unex);
2303 : }
2304 :
2305 : template<typename F>
2306 : constexpr auto
2307 : or_else (F &&f) const &
2308 : {
2309 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (error ())>>;
2310 : static_assert (detail::exp::is_expected_with_value<G, Void>::value,
2311 : "the function passed to std::expected<T, E>::or_else "
2312 : "must return a std::expected with same value type");
2313 : if (has_value ())
2314 : return G ();
2315 : else
2316 : return gcc::invoke (std::forward<F> (f), this->m_unex);
2317 : }
2318 :
2319 : // [expected.void.monadic]p12
2320 : template<typename F>
2321 : constexpr auto
2322 : or_else (F &&f) &&
2323 : {
2324 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
2325 : static_assert (detail::exp::is_expected_with_value<G, Void>::value,
2326 : "the function passed to std::expected<T, E>::or_else "
2327 : "must return a std::expected with same value type");
2328 : if (has_value ())
2329 : return G ();
2330 : else
2331 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
2332 : }
2333 :
2334 : template<typename F>
2335 : constexpr auto
2336 : or_else (F &&f) const &&
2337 : {
2338 : using G = remove_cvref_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
2339 : static_assert (detail::exp::is_expected_with_value<G, Void>::value,
2340 : "the function passed to std::expected<T, E>::or_else "
2341 : "must return a std::expected with same value type");
2342 : if (has_value ())
2343 : return G ();
2344 : else
2345 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
2346 : }
2347 :
2348 : // [expected.void.monadic]p15
2349 : template<typename F,
2350 : typename E_ = E,
2351 : G_Requires<
2352 : std::is_constructible<E_, E_ &>
2353 : > = false>
2354 : constexpr auto
2355 : transform (F &&f) &
2356 : {
2357 : using U = std::remove_cv_t<g_invoke_result_t<F>>;
2358 : using Res = expected<U, E>;
2359 : if (has_value ())
2360 : return Res (detail::exp::in_place_inv{}, [&] () {
2361 : return gcc::invoke (std::forward<F> (f));
2362 : });
2363 : else
2364 : return Res (unexpect, this->m_unex);
2365 : }
2366 :
2367 : template<typename F,
2368 : typename E_ = E,
2369 : G_Requires<
2370 : std::is_constructible<E_, const E_ &>
2371 : > = false>
2372 : constexpr auto
2373 : transform (F &&f) const &
2374 : {
2375 : using U = std::remove_cv_t<g_invoke_result_t<F>>;
2376 : using Res = expected<U, E>;
2377 : if (has_value ())
2378 : return Res (detail::exp::in_place_inv{}, [&] () {
2379 : return gcc::invoke (std::forward<F> (f));
2380 : });
2381 : else
2382 : return Res (unexpect, this->m_unex);
2383 : }
2384 :
2385 : // [expected.void.monadic]p19
2386 : template<typename F,
2387 : typename E_ = E,
2388 : G_Requires<
2389 : std::is_constructible<E_, E_ &&>
2390 : > = false>
2391 : constexpr auto
2392 : transform (F &&f) &&
2393 : {
2394 : using U = std::remove_cv_t<g_invoke_result_t<F>>;
2395 : using Res = expected<U, E>;
2396 : if (has_value ())
2397 : return Res (detail::exp::in_place_inv{}, [&] () {
2398 : return gcc::invoke (std::forward<F> (f));
2399 : });
2400 : else
2401 : return Res (unexpect, std::move (this->m_unex));
2402 : }
2403 :
2404 : template<typename F,
2405 : typename E_ = E,
2406 : G_Requires<
2407 : std::is_constructible<E_, const E_ &&>
2408 : > = false>
2409 : constexpr auto
2410 : transform (F &&f) const &&
2411 : {
2412 : using U = std::remove_cv_t<g_invoke_result_t<F>>;
2413 : using Res = expected<U, E>;
2414 : if (has_value ())
2415 : return Res (detail::exp::in_place_inv{}, [&] () {
2416 : return gcc::invoke (std::forward<F> (f));
2417 : });
2418 : else
2419 : return Res (unexpect, std::move (this->m_unex));
2420 : }
2421 :
2422 : // [expected.void.monadic]p23
2423 : template<typename F>
2424 : constexpr auto
2425 : transform_error (F &&f) &
2426 : {
2427 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (error ())>>;
2428 : using Res = expected<Void, G>;
2429 : if (has_value ())
2430 : return Res ();
2431 : else
2432 : return Res (detail::exp::unexpect_inv{}, [&] () {
2433 : return gcc::invoke (std::forward<F> (f), this->m_unex);
2434 : });
2435 : }
2436 :
2437 : template<typename F>
2438 : constexpr auto
2439 : transform_error (F &&f) const &
2440 : {
2441 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (error ())>>;
2442 : using Res = expected<Void, G>;
2443 : if (has_value ())
2444 : return Res ();
2445 : else
2446 : return Res (detail::exp::unexpect_inv{}, [&] () {
2447 : return gcc::invoke (std::forward<F> (f), this->m_unex);
2448 : });
2449 : }
2450 :
2451 : // [expected.void.monadic]p26
2452 : template<typename F>
2453 : constexpr auto
2454 : transform_error (F &&f) &&
2455 : {
2456 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
2457 : using Res = expected<Void, G>;
2458 : if (has_value ())
2459 : return Res ();
2460 : else
2461 : return Res (detail::exp::unexpect_inv{}, [&] () {
2462 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
2463 : });
2464 : }
2465 :
2466 : template<typename F>
2467 : constexpr auto
2468 : transform_error (F &&f) const &&
2469 : {
2470 : using G = std::remove_cv_t<g_invoke_result_t<F, decltype (std::move (error ()))>>;
2471 : using Res = expected<Void, G>;
2472 : if (has_value ())
2473 : return Res ();
2474 : else
2475 : return Res (detail::exp::unexpect_inv{}, [&] () {
2476 : return gcc::invoke (std::forward<F> (f), std::move (this->m_unex));
2477 : });
2478 : }
2479 :
2480 : // [expected.void.eq]
2481 : template<typename T2, typename E2,
2482 : G_Requires<std::is_void<T2>> = false>
2483 : [[nodiscard]] friend constexpr
2484 : eql_return_t<E, E2>
2485 : operator== (const expected &x, const expected<T2, E2> &y)
2486 : {
2487 : if (x.m_has_value && y.m_has_value)
2488 : return true;
2489 : else if (!x.m_has_value && !y.m_has_value)
2490 : return x.m_unex == y.m_unex;
2491 : return false;
2492 : }
2493 :
2494 : template<typename E2>
2495 : [[nodiscard]] friend constexpr
2496 : eql_return_t<E, E2>
2497 : operator== (const expected &x, const unexpected<E2> &y)
2498 : {
2499 : if (x.m_has_value)
2500 : return false;
2501 : return x.m_unex == y.unex;
2502 : }
2503 :
2504 : private:
2505 : template<typename Fn>
2506 : explicit constexpr
2507 : expected (detail::exp::in_place_inv, Fn&& fn)
2508 : : storage (detail::exp::in_value)
2509 : { std::forward<Fn> (fn) (); }
2510 :
2511 : template<typename Fn>
2512 : explicit constexpr
2513 : expected (detail::exp::unexpect_inv tag, Fn&& fn)
2514 : : storage (tag, std::forward<Fn> (fn))
2515 : {}
2516 :
2517 : template<typename, typename, bool>
2518 : friend struct expected;
2519 : };
2520 :
2521 : // Inequality operators.
2522 : template<typename T1, typename E1, typename T2, typename E2>
2523 : [[nodiscard]] constexpr eql_return_t<expected<T1, E1>, expected<T2, E2>>
2524 : operator!= (const expected<T1, E1>& x, const expected<T2, E2>& y)
2525 : { return !(x == y); }
2526 :
2527 : template<typename T1, typename E1, typename T2>
2528 : [[nodiscard]] constexpr eql_return_t<expected<T1, E1>, T2>
2529 : operator!= (const expected<T1, E1>& x, const T2& y)
2530 : { return !(x == y); }
2531 :
2532 : template<typename T1, typename E1, typename E2>
2533 : [[nodiscard]] constexpr eql_return_t<expected<T1, E1>, unexpected<E2>>
2534 : operator!= (const expected<T1, E1>& x, const unexpected<E2>& y)
2535 : { return !(x == y); }
2536 :
2537 : template<typename E1, typename E2>
2538 : [[nodiscard]] constexpr eql_return_t<unexpected<E1>, unexpected<E2>>
2539 : operator!= (const unexpected<E1>& x, const unexpected<E2>& y)
2540 : {
2541 : return !(x == y);
2542 : }
2543 : } // namespace gcc
2544 :
2545 : #endif // GCC_EXPECTED
|