ThreadSchedule 4.1.0
Modern C++ thread management library
Loading...
Searching...
No Matches
expected.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "detail/lifetime.hpp"
9
10#include <cstddef>
11#include <exception>
12#include <functional>
13#include <initializer_list>
14#include <memory>
15#include <optional>
16#include <system_error>
17#include <type_traits>
18#include <utility>
19#include <variant>
20
21#if defined(__has_include)
22# if __has_include(<version>)
23# include <version>
24# endif
25#endif
26
27#if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202202L
28# include <expected>
29# define THREADSCHEDULE_HAS_STD_EXPECTED 1
30#else
31# define THREADSCHEDULE_HAS_STD_EXPECTED 0
32#endif
33
34#ifdef __cpp_exceptions
35# define THREADSCHEDULE_EXPECTED_THROW(ex) throw ex
36#else
37# define THREADSCHEDULE_EXPECTED_THROW(ex) ::std::terminate()
38#endif
39
40#if defined(__cpp_lib_constexpr_exceptions) && __cpp_lib_constexpr_exceptions >= 202502L
41# define THREADSCHEDULE_EXPECTED_EXCEPTION_CONSTEXPR constexpr
42#else
43# define THREADSCHEDULE_EXPECTED_EXCEPTION_CONSTEXPR
44#endif
45
46namespace threadschedule
47{
48
50{
51 explicit unexpect_t() = default;
52};
53inline constexpr unexpect_t unexpect{};
54
55template <typename E>
57
59template <>
60class bad_expected_access<void> : public std::exception
61{
62protected:
63 bad_expected_access() noexcept = default;
64 bad_expected_access(bad_expected_access const&) noexcept = default;
65 bad_expected_access(bad_expected_access&&) noexcept = default;
66 auto operator=(bad_expected_access const&) noexcept -> bad_expected_access& = default;
67 auto operator=(bad_expected_access&&) noexcept -> bad_expected_access& = default;
69
70public:
72 what() const noexcept -> char const* override
73 {
74 return "bad expected access";
75 }
76};
77
78template <typename E>
79class bad_expected_access : public bad_expected_access<void>
80{
81public:
82 THREADSCHEDULE_EXPECTED_EXCEPTION_CONSTEXPR explicit bad_expected_access(E error) : error_(std::move(error)) {}
84 what() const noexcept -> char const* override
85 {
86 return bad_expected_access<void>::what();
87 }
88 [[nodiscard]] constexpr auto
89 error() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&
90 {
91 return error_;
92 }
93 constexpr auto
94 error() & noexcept THREADSCHEDULE_LIFETIMEBOUND -> E&
95 {
96 return error_;
97 }
98 [[nodiscard]] constexpr auto
99 error() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&&
100 {
101 return std::move(error_);
102 }
103 constexpr auto
104 error() && noexcept THREADSCHEDULE_LIFETIMEBOUND -> E&&
105 {
106 return std::move(error_);
107 }
108
109private:
110 E error_;
111};
113
114template <typename E>
115class unexpected;
116
117namespace detail
118{
119
120template <typename T>
121using expected_remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
122
123template <typename>
124inline constexpr bool is_unexpected_v = false;
125template <typename E>
126inline constexpr bool is_unexpected_v<unexpected<E>> = true;
127
128template <typename E>
129inline constexpr bool valid_expected_error_v = std::is_object_v<E> && !std::is_array_v<E> && !std::is_const_v<E>
130 && !std::is_volatile_v<E> && !is_unexpected_v<E>;
131
132} // namespace detail
133
134template <typename E>
136{
138
139public:
140 unexpected(unexpected const&) = default;
141 unexpected(unexpected&&) = default;
142 auto operator=(unexpected const&) -> unexpected& = default;
143 auto operator=(unexpected&&) -> unexpected& = default;
144
145 template <typename G = E, std::enable_if_t<!std::is_same_v<detail::expected_remove_cvref_t<G>, unexpected>
146 && !std::is_same_v<detail::expected_remove_cvref_t<G>, std::in_place_t>
147 && std::is_constructible_v<E, G>,
148 int> = 0>
149 constexpr explicit unexpected(G&& error) : error_(std::forward<G>(error))
150 {
151 }
152 template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args...>, int> = 0>
153 constexpr explicit unexpected(std::in_place_t, Args&&... args) : error_(std::forward<Args>(args)...)
154 {
155 }
156 template <typename U, typename... Args,
157 std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args...>, int> = 0>
158 constexpr explicit unexpected(std::in_place_t, std::initializer_list<U> list, Args&&... args)
159 : error_(list, std::forward<Args>(args)...)
160 {
161 }
162 [[nodiscard]] constexpr auto
163 error() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&
164 {
165 return error_;
166 }
167 constexpr auto
169 {
170 return error_;
171 }
172 [[nodiscard]] constexpr auto
173 error() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&&
174 {
175 return std::move(error_);
176 }
177 constexpr auto
179 {
180 return std::move(error_);
181 }
182
183 constexpr void
184 swap(unexpected& other) noexcept(std::is_nothrow_swappable_v<E>)
185 {
186 static_assert(std::is_swappable_v<E>);
187 using std::swap;
188 swap(error_, other.error_);
189 }
190
191 template <typename G = E, std::enable_if_t<std::is_swappable_v<E>, G>* = nullptr>
192 friend constexpr void
193 swap(unexpected& lhs, unexpected& rhs) noexcept(noexcept(lhs.swap(rhs)))
194 {
195 lhs.swap(rhs);
196 }
197
198 template <typename G>
199 friend constexpr auto
200 operator==(unexpected const& lhs, unexpected<G> const& rhs) -> bool
201 {
202 return lhs.error() == rhs.error();
203 }
204 template <typename G>
205 friend constexpr auto
206 operator!=(unexpected const& lhs, unexpected<G> const& rhs) -> bool
207 {
208 return !(lhs == rhs);
209 }
210
211private:
212 E error_;
213};
214
215template <typename E>
217
218template <typename T, typename E = std::error_code>
219class expected;
220
221namespace detail
222{
224
225struct expected_value_tag
226{
227};
228struct expected_error_tag
229{
230};
231struct expected_from_other_tag
232{
233};
234struct expected_invoke_tag
235{
236};
237
238template <bool Enabled>
239struct expected_move_constructor
240{
241};
242template <>
243struct expected_move_constructor<false>
244{
245 expected_move_constructor() = default;
246 expected_move_constructor(expected_move_constructor const&) = default;
247 expected_move_constructor(expected_move_constructor&&) = delete;
248 auto operator=(expected_move_constructor const&) -> expected_move_constructor& = default;
249 auto operator=(expected_move_constructor&&) -> expected_move_constructor& = default;
250 ~expected_move_constructor() = default;
251};
252
253constexpr auto
254expected_in_constant_evaluation() noexcept -> bool
255{
256#if defined(_MSC_VER) && !defined(__clang__)
257 return __builtin_is_constant_evaluated();
258#elif defined(__has_builtin)
259# if __has_builtin(__builtin_is_constant_evaluated)
260 return __builtin_is_constant_evaluated();
261# else
262 return false;
263# endif
264#else
265 return false;
266#endif
267}
268
269// Use ADL on the variant pointer to find std::construct_at when available.
270template <std::size_t I, typename Storage, typename... Args>
271constexpr auto
272emplace_expected_storage(Storage& storage, int, Args&&... args) noexcept
273 -> decltype(construct_at(std::addressof(storage), std::in_place_index<I>, std::in_place,
274 std::forward<Args>(args)...),
275 void())
276{
277 if (!expected_in_constant_evaluation())
278 {
279 storage.template emplace<I>(std::in_place, std::forward<Args>(args)...);
280 return;
281 }
282
283 // Reconstructing the whole variant avoids inactive union access in MSVC's constexpr emplace.
284 auto* address = std::addressof(storage);
285 std::destroy_at(address);
286 construct_at(address, std::in_place_index<I>, std::in_place, std::forward<Args>(args)...);
287}
288template <std::size_t I, typename Storage, typename... Args>
289constexpr void
290emplace_expected_storage(Storage& storage, long, Args&&... args) noexcept
291{
292 storage.template emplace<I>(std::in_place, std::forward<Args>(args)...);
293}
294
295template <typename T, typename E>
296class expected_void;
297
298template <typename T>
299inline constexpr bool valid_expected_value_v
300 = std::is_void_v<T>
301 || (std::is_object_v<T> && !std::is_array_v<T> && !std::is_same_v<std::remove_cv_t<T>, std::in_place_t>
302 && !std::is_same_v<std::remove_cv_t<T>, unexpect_t> && !is_unexpected_v<std::remove_cv_t<T>>);
303
304template <typename>
305inline constexpr bool is_expected_v = false;
306template <typename T, typename E>
307inline constexpr bool is_expected_v<expected<T, E>> = true;
308
309template <typename T, typename Source>
310using constructs_from_expected
311 = std::disjunction<std::is_constructible<T, Source&>, std::is_constructible<T, Source const&>,
312 std::is_constructible<T, Source&&>, std::is_constructible<T, Source const&&>>;
313
314template <typename T, typename Source>
315using converts_from_expected = std::disjunction<constructs_from_expected<T, Source>, std::is_convertible<Source&, T>,
316 std::is_convertible<Source const&, T>, std::is_convertible<Source&&, T>,
317 std::is_convertible<Source const&&, T>>;
318
319template <typename T, typename E, typename Source, typename Value, typename Error>
320inline constexpr bool expected_can_convert_v = std::conjunction_v<
321 std::negation<std::is_same<expected<T, E>, Source>>, std::is_constructible<T, Value>,
322 std::is_constructible<E, Error>,
323 std::disjunction<std::is_same<std::remove_cv_t<T>, bool>, std::negation<converts_from_expected<T, Source>>>,
324 std::negation<constructs_from_expected<unexpected<E>, Source>>>;
325
326template <typename T, typename E, typename U>
327inline constexpr bool expected_can_forward_v
328 = !std::is_same_v<std::decay_t<U>, expected<T, E>> && !std::is_same_v<std::decay_t<U>, std::in_place_t>
329 && !std::is_same_v<std::decay_t<U>, unexpect_t> && !is_unexpected_v<expected_remove_cvref_t<U>>
330 && !(std::is_same_v<std::remove_cv_t<T>, bool> && is_expected_v<std::decay_t<U>>)
331 && std::is_constructible_v<T, U>;
332
333template <typename L, typename R, typename = void>
334inline constexpr bool expected_equality_comparable_v = false;
335template <typename L, typename R>
336inline constexpr bool
337 expected_equality_comparable_v<L, R, std::void_t<decltype(std::declval<L const&>() == std::declval<R const&>())>>
338 = std::is_convertible_v<decltype(std::declval<L const&>() == std::declval<R const&>()), bool>;
339
340constexpr void
341require_expected_state(bool valid) noexcept
342{
343 if (!valid)
344 std::terminate();
345}
346
347template <typename F, typename T, typename From>
348using expected_require_constructible
349 = std::enable_if_t<std::conjunction_v<std::is_constructible<T, From>, std::is_same<F, F>>, int>;
350
351template <typename T, typename E>
352inline constexpr bool expected_can_swap_v
353 = std::is_swappable_v<T> && std::is_swappable_v<E> && std::is_move_constructible_v<T>
354 && std::is_move_constructible_v<E>
355 && (std::is_nothrow_move_constructible_v<T> || std::is_nothrow_move_constructible_v<E>);
356
357template <typename T, typename E, typename U, typename G, typename Error>
358inline constexpr bool expected_void_can_convert_v
359 = std::conjunction_v<std::negation<std::is_same<expected<T, E>, expected<U, G>>>, std::is_void<U>,
360 std::is_constructible<E, Error>,
361 std::negation<constructs_from_expected<unexpected<E>, expected<U, G>>>>;
362
363template <typename T>
364struct expected_value_holder
365{
366 template <typename... Args>
367 explicit constexpr expected_value_holder(std::in_place_t,
368 Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args...>)
369 : value(std::forward<Args>(args)...)
370 {
371 }
372 template <typename F, typename... Args>
373 explicit constexpr expected_value_holder(std::in_place_t, expected_invoke_tag, F&& f, Args&&... args)
374 : value(std::invoke(std::forward<F>(f), std::forward<Args>(args)...))
375 {
376 }
377 T value;
378};
379
380template <typename E>
381struct expected_error_holder
382{
383 template <typename... Args>
384 explicit constexpr expected_error_holder(std::in_place_t,
385 Args&&... args) noexcept(std::is_nothrow_constructible_v<E, Args...>)
386 : error(std::forward<Args>(args)...)
387 {
388 }
389 template <typename F, typename... Args>
390 explicit constexpr expected_error_holder(std::in_place_t, expected_invoke_tag, F&& f, Args&&... args)
391 : error(std::invoke(std::forward<F>(f), std::forward<Args>(args)...))
392 {
393 }
394 E error;
395};
396
397template <typename T, typename E>
398class expected_storage_common
399 : private expected_move_constructor<std::is_move_constructible_v<T> && std::is_move_constructible_v<E>>
400{
401public:
402 using value_holder = expected_value_holder<T>;
403 using error_holder = expected_error_holder<E>;
404
405 template <typename... Args>
406 explicit constexpr expected_storage_common(expected_value_tag, Args&&... args)
407 : data_(std::in_place_index<0>, std::in_place, std::forward<Args>(args)...)
408 {
409 }
410 template <typename... Args>
411 explicit constexpr expected_storage_common(expected_error_tag, Args&&... args)
412 : data_(std::in_place_index<1>, std::in_place, std::forward<Args>(args)...)
413 {
414 }
415
416 template <typename Source>
417 explicit constexpr expected_storage_common(expected_from_other_tag, Source&& source)
418 : data_(make_from_other(std::forward<Source>(source)))
419 {
420 }
421
422 expected_storage_common(expected_storage_common const&) = default;
423 expected_storage_common(expected_storage_common&&) = default;
424 auto operator=(expected_storage_common const&) -> expected_storage_common& = default;
425 auto operator=(expected_storage_common&&) -> expected_storage_common& = default;
426 ~expected_storage_common() = default;
427
428 [[nodiscard]] constexpr auto
429 has_value() const noexcept -> bool
430 {
431 return data_.index() == 0;
432 }
433 constexpr auto
434 value() & noexcept THREADSCHEDULE_LIFETIMEBOUND -> T&
435 {
436 return std::get<0>(data_).value;
437 }
438 [[nodiscard]] constexpr auto
439 value() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const&
440 {
441 return std::get<0>(data_).value;
442 }
443 constexpr auto
444 value() && noexcept THREADSCHEDULE_LIFETIMEBOUND -> T&&
445 {
446 return std::move(std::get<0>(data_).value);
447 }
448 [[nodiscard]] constexpr auto
449 value() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const&&
450 {
451 return std::move(std::get<0>(data_).value);
452 }
453 constexpr auto
454 error() & noexcept THREADSCHEDULE_LIFETIMEBOUND -> E&
455 {
456 return std::get<1>(data_).error;
457 }
458 [[nodiscard]] constexpr auto
459 error() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&
460 {
461 return std::get<1>(data_).error;
462 }
463 constexpr auto
464 error() && noexcept THREADSCHEDULE_LIFETIMEBOUND -> E&&
465 {
466 return std::move(std::get<1>(data_).error);
467 }
468 [[nodiscard]] constexpr auto
469 error() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&&
470 {
471 return std::move(std::get<1>(data_).error);
472 }
473
474 template <typename U>
475 constexpr void
476 assign_value(U&& source)
477 {
478 if (has_value())
479 value() = std::forward<U>(source);
480 else
481 replace_with_value(std::forward<U>(source));
482 }
483
484 template <typename G>
485 constexpr void
486 assign_error(G&& source)
487 {
488 if (has_value())
489 replace_with_error(std::forward<G>(source));
490 else
491 error() = std::forward<G>(source);
492 }
493
494 template <typename... Args>
495 constexpr auto
496 emplace_value(Args&&... args) noexcept -> T&
497 {
498 emplace_nothrow<0>(std::forward<Args>(args)...);
499 return value();
500 }
501
502 constexpr void
503 copy_assign(expected_storage_common const& other)
504 {
505 if (has_value() && other.has_value())
506 value() = other.value();
507 else if (!has_value() && !other.has_value())
508 error() = other.error();
509 else if (other.has_value())
510 replace_with_value(other.value());
511 else
512 replace_with_error(other.error());
513 }
514
515 constexpr void
516 move_assign(expected_storage_common&& other)
517 {
518 if (has_value() && other.has_value())
519 value() = std::move(other).value();
520 else if (!has_value() && !other.has_value())
521 error() = std::move(other).error();
522 else if (other.has_value())
523 replace_with_value(std::move(other).value());
524 else
525 replace_with_error(std::move(other).error());
526 }
527
528 constexpr void
529 swap_storage(expected_storage_common& other) noexcept(std::is_nothrow_move_constructible_v<T>
530 && std::is_nothrow_move_constructible_v<E>
531 && std::is_nothrow_swappable_v<T>
532 && std::is_nothrow_swappable_v<E>)
533 {
534 if (has_value() && other.has_value())
535 {
536 using std::swap;
537 swap(value(), other.value());
538 }
539 else if (!has_value() && !other.has_value())
540 {
541 using std::swap;
542 swap(error(), other.error());
543 }
544 else if (has_value())
545 swap_value_error(*this, other);
546 else
547 swap_value_error(other, *this);
548 }
549
550private:
551 using storage_type = std::variant<value_holder, error_holder>;
552
553 template <typename Source>
554 static constexpr auto
555 make_from_other(Source&& source) -> storage_type
556 {
557 if (!source.has_value())
558 return storage_type(std::in_place_index<1>, std::in_place, std::forward<Source>(source).error());
559 return storage_type(std::in_place_index<0>, std::in_place, *std::forward<Source>(source));
560 }
561
562 template <std::size_t I, typename... Args>
563 constexpr void
564 emplace_nothrow(Args&&... args) noexcept
565 {
566 emplace_expected_storage<I>(data_, 0, std::forward<Args>(args)...);
567 }
568
569#ifdef __cpp_exceptions
570 template <std::size_t I, typename Backup, typename... Args>
571# if defined(__cpp_constexpr_exceptions)
572 constexpr
573# endif
574 void
575 emplace_with_rollback(Backup& backup, Args&&... args)
576 {
577 try
578 {
579 data_.template emplace<I>(std::in_place, std::forward<Args>(args)...);
580 }
581 catch (...)
582 {
583 data_.template emplace<1 - I>(std::in_place, std::move(backup));
584 throw;
585 }
586 }
587#endif
588
589 template <std::size_t I, typename Backup, typename... Args>
590 constexpr void
591 emplace_with_backup(Backup&& backup, Args&&... args)
592 {
593#if defined(__cpp_exceptions) && defined(__cpp_constexpr_exceptions)
594 emplace_with_rollback<I>(backup, std::forward<Args>(args)...);
595#else
596# ifdef __cpp_exceptions
597 // Without constexpr exceptions, a throw already makes constant evaluation fail.
598 if (!expected_in_constant_evaluation())
599 {
600 emplace_with_rollback<I>(backup, std::forward<Args>(args)...);
601 return;
602 }
603# endif
604 (void)backup;
605 data_.template emplace<I>(std::in_place, std::forward<Args>(args)...);
606#endif
607 }
608
609 template <typename U>
610 constexpr void
611 replace_with_value(U&& source)
612 {
613 if constexpr (std::is_nothrow_constructible_v<T, U>)
614 emplace_nothrow<0>(std::forward<U>(source));
615 else if constexpr (std::is_nothrow_move_constructible_v<T>)
616 {
617 T replacement(std::forward<U>(source));
618 emplace_nothrow<0>(std::move(replacement));
619 }
620 else
621 {
622 static_assert(std::is_nothrow_move_constructible_v<E>);
623 E backup(std::move(error()));
624 emplace_with_backup<0>(backup, std::forward<U>(source));
625 }
626 }
627
628 template <typename G>
629 constexpr void
630 replace_with_error(G&& source)
631 {
632 if constexpr (std::is_nothrow_constructible_v<E, G>)
633 emplace_nothrow<1>(std::forward<G>(source));
634 else if constexpr (std::is_nothrow_move_constructible_v<E>)
635 {
636 E replacement(std::forward<G>(source));
637 emplace_nothrow<1>(std::move(replacement));
638 }
639 else
640 {
641 static_assert(std::is_nothrow_move_constructible_v<T>);
642 T backup(std::move(value()));
643 emplace_with_backup<1>(backup, std::forward<G>(source));
644 }
645 }
646
647 static constexpr void
648 swap_value_error(expected_storage_common& with_value, expected_storage_common& with_error)
649 {
650 if constexpr (std::is_nothrow_move_constructible_v<E>)
651 {
652 E backup(std::move(with_error.error()));
653 with_error.template emplace_with_backup<0>(backup, std::move(with_value.value()));
654 with_value.template emplace_nothrow<1>(std::move(backup));
655 }
656 else
657 {
658 static_assert(std::is_nothrow_move_constructible_v<T>);
659 T backup(std::move(with_value.value()));
660 with_value.template emplace_with_backup<1>(backup, std::move(with_error.error()));
661 with_error.template emplace_nothrow<0>(std::move(backup));
662 }
663 }
664
665 storage_type data_;
666};
667
668template <typename T, typename E>
669inline constexpr bool expected_copy_assignable_v
670 = std::is_copy_constructible_v<T> && std::is_copy_constructible_v<E> && std::is_copy_assignable_v<T>
671 && std::is_copy_assignable_v<E>
672 && (std::is_nothrow_move_constructible_v<T> || std::is_nothrow_move_constructible_v<E>);
673template <typename T, typename E>
674inline constexpr bool expected_move_assignable_v
675 = std::is_move_constructible_v<T> && std::is_move_constructible_v<E> && std::is_move_assignable_v<T>
676 && std::is_move_assignable_v<E>
677 && (std::is_nothrow_move_constructible_v<T> || std::is_nothrow_move_constructible_v<E>);
678
679enum class expected_assignment
680{
681 unavailable,
682 custom,
683 trivial
684};
685
686template <typename T, typename E>
687inline constexpr auto expected_copy_assignment_v
688 = !expected_copy_assignable_v<T, E> ? expected_assignment::unavailable
689 : std::is_trivially_copy_constructible_v<T> && std::is_trivially_copy_constructible_v<E>
690 && std::is_trivially_copy_assignable_v<T> && std::is_trivially_copy_assignable_v<E>
691 && std::is_trivially_destructible_v<T> && std::is_trivially_destructible_v<E>
692 ? expected_assignment::trivial
693 : expected_assignment::custom;
694
695template <typename T, typename E>
696inline constexpr auto expected_move_assignment_v
697 = !expected_move_assignable_v<T, E> ? expected_assignment::unavailable
698 : std::is_trivially_move_constructible_v<T> && std::is_trivially_move_constructible_v<E>
699 && std::is_trivially_move_assignable_v<T> && std::is_trivially_move_assignable_v<E>
700 && std::is_trivially_destructible_v<T> && std::is_trivially_destructible_v<E>
701 ? expected_assignment::trivial
702 : expected_assignment::custom;
703
704template <typename T, typename E, expected_assignment Copy = expected_copy_assignment_v<T, E>>
705class expected_copy_storage : public expected_storage_common<T, E>
706{
707public:
708 using expected_storage_common<T, E>::expected_storage_common;
709};
710
711template <typename T, typename E>
712class expected_copy_storage<T, E, expected_assignment::custom> : public expected_storage_common<T, E>
713{
714public:
715 using expected_storage_common<T, E>::expected_storage_common;
716 expected_copy_storage(expected_copy_storage const&) = default;
717 expected_copy_storage(expected_copy_storage&&) = default;
718 constexpr auto
719 operator=(expected_copy_storage const& other) -> expected_copy_storage&
720 {
721 this->copy_assign(other);
722 return *this;
723 }
724 auto operator=(expected_copy_storage&&) -> expected_copy_storage& = default;
725 ~expected_copy_storage() = default;
726};
727
728template <typename T, typename E>
729class expected_copy_storage<T, E, expected_assignment::unavailable> : public expected_storage_common<T, E>
730{
731public:
732 using expected_storage_common<T, E>::expected_storage_common;
733 expected_copy_storage(expected_copy_storage const&) = default;
734 expected_copy_storage(expected_copy_storage&&) = default;
735 auto operator=(expected_copy_storage const&) -> expected_copy_storage& = delete;
736 auto operator=(expected_copy_storage&&) -> expected_copy_storage& = default;
737 ~expected_copy_storage() = default;
738};
739
740template <typename T, typename E, expected_assignment Move = expected_move_assignment_v<T, E>>
741class expected_storage : public expected_copy_storage<T, E>
742{
743public:
744 using expected_copy_storage<T, E>::expected_copy_storage;
745};
746
747template <typename T, typename E>
748class expected_storage<T, E, expected_assignment::custom> : public expected_copy_storage<T, E>
749{
750public:
751 using expected_copy_storage<T, E>::expected_copy_storage;
752 expected_storage(expected_storage const&) = default;
753 expected_storage(expected_storage&&) = default;
754 auto operator=(expected_storage const&) -> expected_storage& = default;
755 constexpr auto
756 operator=(expected_storage&& other) noexcept(std::is_nothrow_move_constructible_v<T>
757 && std::is_nothrow_move_constructible_v<E>
758 && std::is_nothrow_move_assignable_v<T>
759 && std::is_nothrow_move_assignable_v<E>) -> expected_storage&
760 {
761 this->move_assign(std::move(other));
762 return *this;
763 }
764 ~expected_storage() = default;
765};
766
767template <typename T, typename E>
768class expected_storage<T, E, expected_assignment::unavailable> : public expected_copy_storage<T, E>
769{
770public:
771 using expected_copy_storage<T, E>::expected_copy_storage;
772 expected_storage(expected_storage const&) = default;
773 expected_storage(expected_storage&&) = default;
774 auto operator=(expected_storage const&) -> expected_storage& = default;
775 auto operator=(expected_storage&&) -> expected_storage& = delete;
776 ~expected_storage() = default;
777};
778
780} // namespace detail
781
782template <typename T, typename E>
784{
785 static_assert(!std::is_void_v<T> && detail::valid_expected_value_v<T>);
787
788public:
789 using value_type = T;
790 using error_type = E;
792 template <typename U>
794
795 template <typename U = T, std::enable_if_t<std::is_default_constructible_v<U>, int> = 0>
796 constexpr expected() : storage_(detail::expected_value_tag{})
797 {
798 }
799 template <typename U = T, std::enable_if_t<!std::is_default_constructible_v<U>, int> = 0>
800 expected() = delete;
801
802 expected(expected const&) = default;
803 expected(expected&&) = default;
804 auto operator=(expected const&) -> expected& = default;
805 auto operator=(expected&&) -> expected& = default;
806 ~expected() = default;
807
808 template <typename U = std::remove_cv_t<T>,
809 std::enable_if_t<detail::expected_can_forward_v<T, E, U> && std::is_convertible_v<U, T>, int> = 0>
810 constexpr expected(U&& value) : storage_(detail::expected_value_tag{}, std::forward<U>(value))
811 {
812 }
813 template <typename U = std::remove_cv_t<T>,
814 std::enable_if_t<detail::expected_can_forward_v<T, E, U> && !std::is_convertible_v<U, T>, int> = 0>
815 constexpr explicit expected(U&& value) : storage_(detail::expected_value_tag{}, std::forward<U>(value))
816 {
817 }
818
819 template <typename U, typename G,
820 std::enable_if_t<detail::expected_can_convert_v<T, E, expected<U, G>, U const&, G const&>
821 && std::is_convertible_v<U const&, T> && std::is_convertible_v<G const&, E>,
822 int> = 0>
823 constexpr expected(expected<U, G> const& source) : storage_(detail::expected_from_other_tag{}, source)
824 {
825 }
826 template <typename U, typename G,
827 std::enable_if_t<detail::expected_can_convert_v<T, E, expected<U, G>, U const&, G const&>
828 && !(std::is_convertible_v<U const&, T> && std::is_convertible_v<G const&, E>),
829 int> = 0>
830 constexpr explicit expected(expected<U, G> const& source) : storage_(detail::expected_from_other_tag{}, source)
831 {
832 }
833 template <typename U, typename G,
834 std::enable_if_t<detail::expected_can_convert_v<T, E, expected<U, G>, U, G> && std::is_convertible_v<U, T>
835 && std::is_convertible_v<G, E>,
836 int> = 0>
837 constexpr expected(expected<U, G>&& source) : storage_(detail::expected_from_other_tag{}, std::move(source))
838 {
839 }
840 template <typename U, typename G,
841 std::enable_if_t<detail::expected_can_convert_v<T, E, expected<U, G>, U, G>
842 && !(std::is_convertible_v<U, T> && std::is_convertible_v<G, E>),
843 int> = 0>
844 constexpr explicit expected(expected<U, G>&& source) : storage_(detail::expected_from_other_tag{}, std::move(source))
845 {
846 }
847
848 template <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args...>, int> = 0>
849 constexpr explicit expected(std::in_place_t, Args&&... args)
850 : storage_(detail::expected_value_tag{}, std::forward<Args>(args)...)
851 {
852 }
853 template <typename U, typename... Args,
854 std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>, int> = 0>
855 constexpr explicit expected(std::in_place_t, std::initializer_list<U> list, Args&&... args)
856 : storage_(detail::expected_value_tag{}, list, std::forward<Args>(args)...)
857 {
858 }
859 template <typename G,
860 std::enable_if_t<std::is_constructible_v<E, G const&> && std::is_convertible_v<G const&, E>, int> = 0>
861 constexpr expected(unexpected<G> const& source) : storage_(detail::expected_error_tag{}, source.error())
862 {
863 }
864 template <typename G,
865 std::enable_if_t<std::is_constructible_v<E, G const&> && !std::is_convertible_v<G const&, E>, int> = 0>
866 constexpr explicit expected(unexpected<G> const& source) : storage_(detail::expected_error_tag{}, source.error())
867 {
868 }
869 template <typename G, std::enable_if_t<std::is_constructible_v<E, G> && std::is_convertible_v<G, E>, int> = 0>
870 constexpr expected(unexpected<G>&& source) : storage_(detail::expected_error_tag{}, std::move(source).error())
871 {
872 }
873 template <typename G, std::enable_if_t<std::is_constructible_v<E, G> && !std::is_convertible_v<G, E>, int> = 0>
874 constexpr explicit expected(unexpected<G>&& source)
875 : storage_(detail::expected_error_tag{}, std::move(source).error())
876 {
877 }
878 template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args...>, int> = 0>
879 constexpr explicit expected(unexpect_t, Args&&... args)
880 : storage_(detail::expected_error_tag{}, std::forward<Args>(args)...)
881 {
882 }
883
884 template <typename U, typename... Args,
885 std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args...>, int> = 0>
886 constexpr explicit expected(unexpect_t, std::initializer_list<U> list, Args&&... args)
887 : storage_(detail::expected_error_tag{}, list, std::forward<Args>(args)...)
888 {
889 }
890
891 template <typename U = std::remove_cv_t<T>,
892 std::enable_if_t<!std::is_same_v<detail::expected_remove_cvref_t<U>, expected>
893 && !detail::is_unexpected_v<detail::expected_remove_cvref_t<U>>
894 && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U>
895 && (std::is_nothrow_constructible_v<T, U> || std::is_nothrow_move_constructible_v<T>
896 || std::is_nothrow_move_constructible_v<E>),
897 int> = 0>
898 constexpr auto
900 {
901 storage_.assign_value(std::forward<U>(value));
902 return *this;
903 }
904 template <typename G, std::enable_if_t<std::is_constructible_v<E, G const&> && std::is_assignable_v<E&, G const&>
905 && (std::is_nothrow_constructible_v<E, G const&>
906 || std::is_nothrow_move_constructible_v<E>
907 || std::is_nothrow_move_constructible_v<T>),
908 int> = 0>
909 constexpr auto
911 {
912 storage_.assign_error(error.error());
913 return *this;
914 }
915 template <typename G,
916 std::enable_if_t<std::is_constructible_v<E, G> && std::is_assignable_v<E&, G>
917 && (std::is_nothrow_constructible_v<E, G> || std::is_nothrow_move_constructible_v<E>
918 || std::is_nothrow_move_constructible_v<T>),
919 int> = 0>
920 constexpr auto
921 operator=(unexpected<G>&& error) -> expected&
922 {
923 storage_.assign_error(std::move(error).error());
924 return *this;
925 }
926
927 [[nodiscard]] constexpr auto
928 has_value() const noexcept -> bool
929 {
930 return storage_.has_value();
931 }
932 [[nodiscard]] constexpr auto
933 has_error() const noexcept -> bool
934 {
935 return !has_value();
936 }
937 constexpr explicit
938 operator bool() const noexcept
939 {
940 return has_value();
941 }
942 constexpr auto
944 {
945 return std::addressof(storage_.value());
946 }
947 constexpr auto
948 operator->() const noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const*
949 {
950 return std::addressof(storage_.value());
951 }
952 constexpr auto
954 {
955 return storage_.value();
956 }
957 [[nodiscard]] constexpr auto
958 operator*() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const&
959 {
960 return storage_.value();
961 }
962 constexpr auto
964 {
965 return std::move(storage_).value();
966 }
967 [[nodiscard]] constexpr auto
968 operator*() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const&&
969 {
970 return std::move(storage_).value();
971 }
972
973 constexpr auto
975 {
976 static_assert(std::is_copy_constructible_v<E>);
977 if (!has_value())
979 return storage_.value();
980 }
981 [[nodiscard]] constexpr auto
983 {
984 static_assert(std::is_copy_constructible_v<E>);
985 if (!has_value())
987 return storage_.value();
988 }
989 constexpr auto
991 {
992 static_assert(std::is_copy_constructible_v<E> && std::is_constructible_v<E, E&&>);
993 if (!has_value())
995 return std::move(storage_).value();
996 }
997 [[nodiscard]] constexpr auto
998 value() const&& THREADSCHEDULE_LIFETIMEBOUND -> T const&&
999 {
1000 static_assert(std::is_copy_constructible_v<E> && std::is_constructible_v<E, E const&&>);
1001 if (!has_value())
1003 return std::move(storage_).value();
1004 }
1005 constexpr auto
1007 {
1008 return storage_.error();
1009 }
1010 [[nodiscard]] constexpr auto
1011 error() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&
1012 {
1013 return storage_.error();
1014 }
1015 constexpr auto
1017 {
1018 return std::move(storage_).error();
1019 }
1020 [[nodiscard]] constexpr auto
1021 error() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&&
1022 {
1023 return std::move(storage_).error();
1024 }
1025
1026 template <typename U = std::remove_cv_t<T>>
1027 constexpr auto
1028 value_or(U&& fallback) const& -> T
1029 {
1030 static_assert(std::is_copy_constructible_v<T> && std::is_convertible_v<U, T>);
1031 return has_value() ? storage_.value() : static_cast<T>(std::forward<U>(fallback));
1032 }
1033 template <typename U = std::remove_cv_t<T>>
1034 constexpr auto
1035 value_or(U&& fallback) && -> T
1036 {
1037 static_assert(std::is_move_constructible_v<T> && std::is_convertible_v<U, T>);
1038 return has_value() ? std::move(storage_).value() : static_cast<T>(std::forward<U>(fallback));
1039 }
1040 template <typename G = E>
1041 constexpr auto
1042 error_or(G&& fallback) const& -> E
1043 {
1044 static_assert(std::is_copy_constructible_v<E> && std::is_convertible_v<G, E>);
1045 if (has_value())
1046 return std::forward<G>(fallback);
1047 return storage_.error();
1048 }
1049 template <typename G = E>
1050 constexpr auto
1051 error_or(G&& fallback) && -> E
1052 {
1053 static_assert(std::is_move_constructible_v<E> && std::is_convertible_v<G, E>);
1054 if (has_value())
1055 return std::forward<G>(fallback);
1056 return std::move(storage_).error();
1057 }
1058 template <typename... Args, std::enable_if_t<std::is_nothrow_constructible_v<T, Args...>, int> = 0>
1059 constexpr auto
1060 emplace(Args&&... args) noexcept -> T&
1061 {
1062 return storage_.emplace_value(std::forward<Args>(args)...);
1063 }
1064 template <typename U, typename... Args,
1065 std::enable_if_t<std::is_nothrow_constructible_v<T, std::initializer_list<U>&, Args...>, int> = 0>
1066 constexpr auto
1067 emplace(std::initializer_list<U> list, Args&&... args) noexcept -> T&
1068 {
1069 return storage_.emplace_value(list, std::forward<Args>(args)...);
1070 }
1071 template <typename U = T, std::enable_if_t<detail::expected_can_swap_v<T, E>, U>* = nullptr>
1072 constexpr void
1073 swap(expected& other) noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_constructible_v<E>
1074 && std::is_nothrow_swappable_v<T> && std::is_nothrow_swappable_v<E>)
1075 {
1076 storage_.swap_storage(other.storage_);
1077 }
1078
1079 template <typename F, detail::expected_require_constructible<F, E, E&> = 0>
1080 constexpr auto
1081 and_then(F&& f) &
1082 {
1083 return and_then_impl(*this, std::forward<F>(f));
1084 }
1085 template <typename F, detail::expected_require_constructible<F, E, E const&> = 0>
1086 constexpr auto
1087 and_then(F&& f) const&
1088 {
1089 return and_then_impl(*this, std::forward<F>(f));
1090 }
1091 template <typename F, detail::expected_require_constructible<F, E, E&&> = 0>
1092 constexpr auto
1093 and_then(F&& f) &&
1094 {
1095 return and_then_impl(std::move(*this), std::forward<F>(f));
1096 }
1097 template <typename F, detail::expected_require_constructible<F, E, E const&&> = 0>
1098 constexpr auto
1099 and_then(F&& f) const&&
1100 {
1101 return and_then_impl(std::move(*this), std::forward<F>(f));
1102 }
1103 template <typename F, detail::expected_require_constructible<F, T, T&> = 0>
1104 constexpr auto
1105 or_else(F&& f) &
1106 {
1107 return or_else_impl(*this, std::forward<F>(f));
1108 }
1109 template <typename F, detail::expected_require_constructible<F, T, T const&> = 0>
1110 constexpr auto
1111 or_else(F&& f) const&
1112 {
1113 return or_else_impl(*this, std::forward<F>(f));
1114 }
1115 template <typename F, detail::expected_require_constructible<F, T, T&&> = 0>
1116 constexpr auto
1117 or_else(F&& f) &&
1118 {
1119 return or_else_impl(std::move(*this), std::forward<F>(f));
1120 }
1121 template <typename F, detail::expected_require_constructible<F, T, T const&&> = 0>
1122 constexpr auto
1123 or_else(F&& f) const&&
1124 {
1125 return or_else_impl(std::move(*this), std::forward<F>(f));
1126 }
1127 template <typename F, detail::expected_require_constructible<F, E, E&> = 0>
1128 constexpr auto
1129 transform(F&& f) &
1130 {
1131 return transform_impl(*this, std::forward<F>(f));
1132 }
1133 template <typename F, detail::expected_require_constructible<F, E, E const&> = 0>
1134 constexpr auto
1135 transform(F&& f) const&
1136 {
1137 return transform_impl(*this, std::forward<F>(f));
1138 }
1139 template <typename F, detail::expected_require_constructible<F, E, E&&> = 0>
1140 constexpr auto
1141 transform(F&& f) &&
1142 {
1143 return transform_impl(std::move(*this), std::forward<F>(f));
1144 }
1145 template <typename F, detail::expected_require_constructible<F, E, E const&&> = 0>
1146 constexpr auto
1147 transform(F&& f) const&&
1148 {
1149 return transform_impl(std::move(*this), std::forward<F>(f));
1150 }
1151 template <typename F, detail::expected_require_constructible<F, T, T&> = 0>
1152 constexpr auto
1154 {
1155 return transform_error_impl(*this, std::forward<F>(f));
1156 }
1157 template <typename F, detail::expected_require_constructible<F, T, T const&> = 0>
1158 constexpr auto
1159 transform_error(F&& f) const&
1160 {
1161 return transform_error_impl(*this, std::forward<F>(f));
1162 }
1163 template <typename F, detail::expected_require_constructible<F, T, T&&> = 0>
1164 constexpr auto
1166 {
1167 return transform_error_impl(std::move(*this), std::forward<F>(f));
1168 }
1169 template <typename F, detail::expected_require_constructible<F, T, T const&&> = 0>
1170 constexpr auto
1171 transform_error(F&& f) const&&
1172 {
1173 return transform_error_impl(std::move(*this), std::forward<F>(f));
1174 }
1175
1176#if THREADSCHEDULE_HAS_STD_EXPECTED
1177 template <typename U = T, typename G = E,
1178 std::enable_if_t<std::is_copy_constructible_v<U> && std::is_copy_constructible_v<G>, int> = 0>
1179 constexpr
1180 operator std::expected<T, E>() const&
1181 {
1182 if (has_value())
1183 return std::expected<T, E>(std::in_place, storage_.value());
1184 return std::expected<T, E>(std::unexpect, storage_.error());
1185 }
1186 template <typename U = T, typename G = E,
1187 std::enable_if_t<std::is_move_constructible_v<U> && std::is_move_constructible_v<G>, int> = 0>
1188 constexpr
1189 operator std::expected<T, E>() &&
1190 {
1191 if (has_value())
1192 return std::expected<T, E>(std::in_place, std::move(storage_).value());
1193 return std::expected<T, E>(std::unexpect, std::move(storage_).error());
1194 }
1195#endif
1196
1197 template <typename T2, typename E2,
1198 std::enable_if_t<!std::is_void_v<T2> && detail::expected_equality_comparable_v<T, T2>
1199 && detail::expected_equality_comparable_v<E, E2>,
1200 int> = 0>
1201 friend constexpr auto
1202 operator==(expected const& lhs, expected<T2, E2> const& rhs) -> bool
1203 {
1204 if (lhs.has_value() != rhs.has_value())
1205 return false;
1206 if (lhs.has_value())
1207 return *lhs == *rhs;
1208 return lhs.error() == rhs.error();
1209 }
1210 template <typename T2, typename E2,
1211 std::enable_if_t<!std::is_void_v<T2> && detail::expected_equality_comparable_v<T, T2>
1212 && detail::expected_equality_comparable_v<E, E2>,
1213 int> = 0>
1214 friend constexpr auto
1215 operator!=(expected const& lhs, expected<T2, E2> const& rhs) -> bool
1216 {
1217 return !(lhs == rhs);
1218 }
1219 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1220 friend constexpr auto
1221 operator==(expected const& lhs, unexpected<E2> const& rhs) -> bool
1222 {
1223 if (lhs.has_value())
1224 return false;
1225 return lhs.error() == rhs.error();
1226 }
1227 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1228 friend constexpr auto
1229 operator==(unexpected<E2> const& lhs, expected const& rhs) -> bool
1230 {
1231 return rhs == lhs;
1232 }
1233 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1234 friend constexpr auto
1235 operator!=(expected const& lhs, unexpected<E2> const& rhs) -> bool
1236 {
1237 return !(lhs == rhs);
1238 }
1239 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1240 friend constexpr auto
1241 operator!=(unexpected<E2> const& lhs, expected const& rhs) -> bool
1242 {
1243 return !(rhs == lhs);
1244 }
1245
1246private:
1247 template <typename, typename>
1248 friend class expected;
1249 template <typename, typename>
1251
1252 template <typename Tag, typename F, typename... Args>
1253 constexpr explicit expected(Tag tag, detail::expected_invoke_tag, F&& f, Args&&... args)
1254 : storage_(tag, detail::expected_invoke_tag{}, std::forward<F>(f), std::forward<Args>(args)...)
1255 {
1256 }
1257
1258 template <typename Self, typename F>
1259 static constexpr auto
1260 and_then_impl(Self&& self, F&& f)
1261 {
1262 using result_type
1264 static_assert(detail::is_expected_v<result_type> && std::is_same_v<typename result_type::error_type, E>);
1265 if (self.has_value())
1266 return result_type(std::invoke(std::forward<F>(f), std::forward<Self>(self).storage_.value()));
1267 return result_type(unexpect, std::forward<Self>(self).error());
1268 }
1269 template <typename Self, typename F>
1270 static constexpr auto
1271 or_else_impl(Self&& self, F&& f)
1272 {
1273 using result_type
1275 static_assert(detail::is_expected_v<result_type> && std::is_same_v<typename result_type::value_type, T>);
1276 if (self.has_value())
1277 return result_type(std::in_place, std::forward<Self>(self).storage_.value());
1278 return result_type(std::invoke(std::forward<F>(f), std::forward<Self>(self).error()));
1279 }
1280 template <typename Self, typename F>
1281 static constexpr auto
1282 transform_impl(Self&& self, F&& f)
1283 {
1284 using result_value = std::remove_cv_t<std::invoke_result_t<F, decltype(std::forward<Self>(self).storage_.value())>>;
1285 static_assert(detail::valid_expected_value_v<result_value>);
1286 using result_type = expected<result_value, E>;
1287 if (!self.has_value())
1288 return result_type(unexpect, std::forward<Self>(self).error());
1289 if constexpr (std::is_void_v<result_value>)
1290 {
1291 std::invoke(std::forward<F>(f), std::forward<Self>(self).storage_.value());
1292 return result_type();
1293 }
1294 else
1295 return result_type(detail::expected_value_tag{}, detail::expected_invoke_tag{}, std::forward<F>(f),
1296 std::forward<Self>(self).storage_.value());
1297 }
1298 template <typename Self, typename F>
1299 static constexpr auto
1300 transform_error_impl(Self&& self, F&& f)
1301 {
1302 using result_error = std::remove_cv_t<std::invoke_result_t<F, decltype(std::forward<Self>(self).error())>>;
1304 using result_type = expected<T, result_error>;
1305 if (self.has_value())
1306 return result_type(std::in_place, std::forward<Self>(self).storage_.value());
1307 return result_type(detail::expected_error_tag{}, detail::expected_invoke_tag{}, std::forward<F>(f),
1308 std::forward<Self>(self).error());
1309 }
1310
1311 detail::expected_storage<T, E> storage_;
1312};
1313
1314namespace detail
1315{
1316
1317template <typename T, typename E>
1319{
1320 static_assert(std::is_void_v<T> && valid_expected_error_v<E>);
1321 using storage_type = std::optional<expected_error_holder<E>>;
1322 using derived_type = expected<T, E>;
1323
1324public:
1325 using value_type = T;
1326 using error_type = E;
1328
1329 template <typename U>
1331
1332 constexpr expected_void() noexcept = default;
1333 constexpr explicit expected_void(std::in_place_t) noexcept {}
1334
1335 template <
1336 typename U, typename G,
1337 std::enable_if_t<detail::expected_void_can_convert_v<T, E, U, G, G const&> && std::is_convertible_v<G const&, E>,
1338 int> = 0>
1339 constexpr expected_void(expected<U, G> const& source) : storage_(make_error_from(source))
1340 {
1341 }
1342 template <
1343 typename U, typename G,
1344 std::enable_if_t<detail::expected_void_can_convert_v<T, E, U, G, G const&> && !std::is_convertible_v<G const&, E>,
1345 int> = 0>
1346 constexpr explicit expected_void(expected<U, G> const& source) : storage_(make_error_from(source))
1347 {
1348 }
1349 template <typename U, typename G,
1350 std::enable_if_t<detail::expected_void_can_convert_v<T, E, U, G, G> && std::is_convertible_v<G, E>, int>
1351 = 0>
1352 constexpr expected_void(expected<U, G>&& source) : storage_(make_error_from(std::move(source)))
1353 {
1354 }
1355 template <typename U, typename G,
1356 std::enable_if_t<detail::expected_void_can_convert_v<T, E, U, G, G> && !std::is_convertible_v<G, E>, int>
1357 = 0>
1358 constexpr explicit expected_void(expected<U, G>&& source) : storage_(make_error_from(std::move(source)))
1359 {
1360 }
1361
1362 template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args...>, int> = 0>
1363 constexpr explicit expected_void(unexpect_t, Args&&... args)
1364 : storage_(std::in_place, std::in_place, std::forward<Args>(args)...)
1365 {
1366 }
1367 template <typename G,
1368 std::enable_if_t<std::is_constructible_v<E, G const&> && std::is_convertible_v<G const&, E>, int> = 0>
1369 constexpr expected_void(unexpected<G> const& source) : storage_(std::in_place, std::in_place, source.error())
1370 {
1371 }
1372 template <typename G,
1373 std::enable_if_t<std::is_constructible_v<E, G const&> && !std::is_convertible_v<G const&, E>, int> = 0>
1374 constexpr explicit expected_void(unexpected<G> const& source) : storage_(std::in_place, std::in_place, source.error())
1375 {
1376 }
1377 template <typename G, std::enable_if_t<std::is_constructible_v<E, G> && std::is_convertible_v<G, E>, int> = 0>
1378 constexpr expected_void(unexpected<G>&& source) : storage_(std::in_place, std::in_place, std::move(source).error())
1379 {
1380 }
1381 template <typename G, std::enable_if_t<std::is_constructible_v<E, G> && !std::is_convertible_v<G, E>, int> = 0>
1382 constexpr explicit expected_void(unexpected<G>&& source)
1383 : storage_(std::in_place, std::in_place, std::move(source).error())
1384 {
1385 }
1386 template <typename U, typename... Args,
1387 std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args...>, int> = 0>
1388 constexpr explicit expected_void(unexpect_t, std::initializer_list<U> list, Args&&... args)
1389 : storage_(std::in_place, std::in_place, list, std::forward<Args>(args)...)
1390 {
1391 }
1392 template <typename G,
1393 std::enable_if_t<std::is_constructible_v<E, G const&> && std::is_assignable_v<E&, G const&>, int> = 0>
1394 constexpr auto
1395 operator=(unexpected<G> const& error) -> derived_type&
1396 {
1397 assign_error(error.error());
1398 return static_cast<derived_type&>(*this);
1399 }
1400 template <typename G, std::enable_if_t<std::is_constructible_v<E, G> && std::is_assignable_v<E&, G>, int> = 0>
1401 constexpr auto
1402 operator=(unexpected<G>&& error) -> derived_type&
1403 {
1404 assign_error(std::move(error).error());
1405 return static_cast<derived_type&>(*this);
1406 }
1407
1408 [[nodiscard]] constexpr auto
1409 has_value() const noexcept -> bool
1410 {
1411 return !storage_.has_value();
1412 }
1413 [[nodiscard]] constexpr auto
1414 has_error() const noexcept -> bool
1415 {
1416 return !has_value();
1417 }
1418 constexpr explicit
1419 operator bool() const noexcept
1420 {
1421 return has_value();
1422 }
1423 constexpr void
1424 operator*() const noexcept
1425 {
1426 require_expected_state(has_value());
1427 }
1428 constexpr void
1429 value() const&
1430 {
1431 if (!has_value())
1433 }
1434 constexpr void
1436 {
1437 if (!has_value())
1438 THREADSCHEDULE_EXPECTED_THROW(bad_expected_access<E>(std::move(storage_->error)));
1439 }
1440 constexpr auto
1442 {
1443 require_expected_state(has_error());
1444 return storage_->error;
1445 }
1446 [[nodiscard]] constexpr auto
1447 error() const& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&
1448 {
1449 require_expected_state(has_error());
1450 return storage_->error;
1451 }
1452 constexpr auto
1454 {
1455 require_expected_state(has_error());
1456 return std::move(storage_->error);
1457 }
1458 [[nodiscard]] constexpr auto
1459 error() const&& noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const&&
1460 {
1461 require_expected_state(has_error());
1462 return std::move(storage_->error);
1463 }
1464 template <typename G = E>
1465 constexpr auto
1466 error_or(G&& fallback) const& -> E
1467 {
1468 static_assert(std::is_copy_constructible_v<E> && std::is_convertible_v<G, E>);
1469 if (has_value())
1470 return std::forward<G>(fallback);
1471 return storage_->error;
1472 }
1473 template <typename G = E>
1474 constexpr auto
1475 error_or(G&& fallback) && -> E
1476 {
1477 static_assert(std::is_move_constructible_v<E> && std::is_convertible_v<G, E>);
1478 if (has_value())
1479 return std::forward<G>(fallback);
1480 return std::move(storage_->error);
1481 }
1482 constexpr void
1483 emplace() noexcept
1484 {
1485 storage_.reset();
1486 }
1487 template <typename G = E, std::enable_if_t<std::is_move_constructible_v<E> && std::is_swappable_v<E>, G>* = nullptr>
1488 constexpr void
1489 swap(derived_type& other) noexcept(std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E>)
1490 {
1491 if (storage_ && other.storage_)
1492 {
1493 using std::swap;
1494 swap(storage_->error, other.storage_->error);
1495 }
1496 else if (storage_)
1497 other.move_error_from(*this);
1498 else if (other.storage_)
1499 move_error_from(other);
1500 }
1501
1502 template <typename F, detail::expected_require_constructible<F, E, E&> = 0>
1503 constexpr auto
1504 and_then(F&& f) &
1505 {
1506 return and_then_impl(*this, std::forward<F>(f));
1507 }
1508 template <typename F, detail::expected_require_constructible<F, E, E const&> = 0>
1509 constexpr auto
1510 and_then(F&& f) const&
1511 {
1512 return and_then_impl(*this, std::forward<F>(f));
1513 }
1514 template <typename F, detail::expected_require_constructible<F, E, E&&> = 0>
1515 constexpr auto
1516 and_then(F&& f) &&
1517 {
1518 return and_then_impl(std::move(*this), std::forward<F>(f));
1519 }
1520 template <typename F, detail::expected_require_constructible<F, E, E const&&> = 0>
1521 constexpr auto
1522 and_then(F&& f) const&&
1523 {
1524 return and_then_impl(std::move(*this), std::forward<F>(f));
1525 }
1526 template <typename F>
1527 constexpr auto
1528 or_else(F&& f) &
1529 {
1530 return or_else_impl(*this, std::forward<F>(f));
1531 }
1532 template <typename F>
1533 constexpr auto
1534 or_else(F&& f) const&
1535 {
1536 return or_else_impl(*this, std::forward<F>(f));
1537 }
1538 template <typename F>
1539 constexpr auto
1540 or_else(F&& f) &&
1541 {
1542 return or_else_impl(std::move(*this), std::forward<F>(f));
1543 }
1544 template <typename F>
1545 constexpr auto
1546 or_else(F&& f) const&&
1547 {
1548 return or_else_impl(std::move(*this), std::forward<F>(f));
1549 }
1550 template <typename F, detail::expected_require_constructible<F, E, E&> = 0>
1551 constexpr auto
1552 transform(F&& f) &
1553 {
1554 return transform_impl(*this, std::forward<F>(f));
1555 }
1556 template <typename F, detail::expected_require_constructible<F, E, E const&> = 0>
1557 constexpr auto
1558 transform(F&& f) const&
1559 {
1560 return transform_impl(*this, std::forward<F>(f));
1561 }
1562 template <typename F, detail::expected_require_constructible<F, E, E&&> = 0>
1563 constexpr auto
1564 transform(F&& f) &&
1565 {
1566 return transform_impl(std::move(*this), std::forward<F>(f));
1567 }
1568 template <typename F, detail::expected_require_constructible<F, E, E const&&> = 0>
1569 constexpr auto
1570 transform(F&& f) const&&
1571 {
1572 return transform_impl(std::move(*this), std::forward<F>(f));
1573 }
1574 template <typename F>
1575 constexpr auto
1577 {
1578 return transform_error_impl(*this, std::forward<F>(f));
1579 }
1580 template <typename F>
1581 constexpr auto
1582 transform_error(F&& f) const&
1583 {
1584 return transform_error_impl(*this, std::forward<F>(f));
1585 }
1586 template <typename F>
1587 constexpr auto
1589 {
1590 return transform_error_impl(std::move(*this), std::forward<F>(f));
1591 }
1592 template <typename F>
1593 constexpr auto
1594 transform_error(F&& f) const&&
1595 {
1596 return transform_error_impl(std::move(*this), std::forward<F>(f));
1597 }
1598
1599#if THREADSCHEDULE_HAS_STD_EXPECTED
1600 template <typename G = E, std::enable_if_t<std::is_copy_constructible_v<G>, int> = 0>
1601 constexpr
1602 operator std::expected<T, E>() const&
1603 {
1604 if (has_value())
1605 return std::expected<T, E>();
1606 return std::expected<T, E>(std::unexpect, storage_->error);
1607 }
1608 template <typename G = E, std::enable_if_t<std::is_move_constructible_v<G>, int> = 0>
1609 constexpr
1610 operator std::expected<T, E>() &&
1611 {
1612 if (has_value())
1613 return std::expected<T, E>();
1614 return std::expected<T, E>(std::unexpect, std::move(storage_->error));
1615 }
1616#endif
1617
1618 template <typename T2, typename E2,
1619 std::enable_if_t<std::is_void_v<T2> && expected_equality_comparable_v<E, E2>, int> = 0>
1620 friend constexpr auto
1621 operator==(derived_type const& lhs, expected<T2, E2> const& rhs) -> bool
1622 {
1623 if (lhs.has_value() != rhs.has_value())
1624 return false;
1625 if (lhs.has_value())
1626 return true;
1627 return lhs.error() == rhs.error();
1628 }
1629 template <typename T2, typename E2,
1630 std::enable_if_t<std::is_void_v<T2> && expected_equality_comparable_v<E, E2>, int> = 0>
1631 friend constexpr auto
1632 operator!=(derived_type const& lhs, expected<T2, E2> const& rhs) -> bool
1633 {
1634 return !(lhs == rhs);
1635 }
1636 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1637 friend constexpr auto
1638 operator==(derived_type const& lhs, unexpected<E2> const& rhs) -> bool
1639 {
1640 if (lhs.has_value())
1641 return false;
1642 return lhs.error() == rhs.error();
1643 }
1644 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1645 friend constexpr auto
1646 operator==(unexpected<E2> const& lhs, derived_type const& rhs) -> bool
1647 {
1648 return rhs == lhs;
1649 }
1650 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1651 friend constexpr auto
1652 operator!=(derived_type const& lhs, unexpected<E2> const& rhs) -> bool
1653 {
1654 return !(lhs == rhs);
1655 }
1656 template <typename E2, std::enable_if_t<detail::expected_equality_comparable_v<E, E2>, int> = 0>
1657 friend constexpr auto
1658 operator!=(unexpected<E2> const& lhs, derived_type const& rhs) -> bool
1659 {
1660 return !(rhs == lhs);
1661 }
1662
1663private:
1664 template <typename Self, typename F>
1665 static constexpr auto
1666 and_then_impl(Self&& self, F&& f)
1667 {
1669 static_assert(is_expected_v<result_type> && std::is_same_v<typename result_type::error_type, E>);
1670 if (self.has_value())
1671 return result_type(std::invoke(std::forward<F>(f)));
1672 return result_type(unexpect, std::forward<Self>(self).error());
1673 }
1674 template <typename Self, typename F>
1675 static constexpr auto
1676 or_else_impl(Self&& self, F&& f)
1677 {
1679 static_assert(is_expected_v<result_type> && std::is_same_v<typename result_type::value_type, T>);
1680 if (self.has_value())
1681 return result_type();
1682 return result_type(std::invoke(std::forward<F>(f), std::forward<Self>(self).error()));
1683 }
1684 template <typename Self, typename F>
1685 static constexpr auto
1686 transform_impl(Self&& self, F&& f)
1687 {
1688 using result_value = std::remove_cv_t<std::invoke_result_t<F>>;
1689 static_assert(valid_expected_value_v<result_value>);
1690 using result_type = expected<result_value, E>;
1691 if (!self.has_value())
1692 return result_type(unexpect, std::forward<Self>(self).error());
1693 if constexpr (std::is_void_v<result_value>)
1694 {
1695 std::invoke(std::forward<F>(f));
1696 return result_type();
1697 }
1698 else
1699 return result_type(expected_value_tag{}, expected_invoke_tag{}, std::forward<F>(f));
1700 }
1701 template <typename Self, typename F>
1702 static constexpr auto
1703 transform_error_impl(Self&& self, F&& f)
1704 {
1705 using result_error = std::remove_cv_t<std::invoke_result_t<F, decltype(std::forward<Self>(self).error())>>;
1707 using result_type = expected<T, result_error>;
1708 if (self.has_value())
1709 return result_type();
1710 return result_type(expected_error_tag{}, expected_invoke_tag{}, std::forward<F>(f),
1711 std::forward<Self>(self).error());
1712 }
1713
1714 template <typename, typename>
1716 template <typename, typename>
1717 friend class expected_void;
1718
1719 template <typename F, typename... Args>
1720 constexpr explicit expected_void(expected_error_tag, expected_invoke_tag, F&& f, Args&&... args)
1721 : storage_(std::in_place, std::in_place, expected_invoke_tag{}, std::forward<F>(f), std::forward<Args>(args)...)
1722 {
1723 }
1724
1725 template <typename Source>
1726 static constexpr auto
1727 make_error_from(Source&& source) -> storage_type
1728 {
1729 if (source.has_value())
1730 return std::nullopt;
1731 return storage_type(std::in_place, std::in_place, std::forward<Source>(source).error());
1732 }
1733
1734 template <typename G>
1735 constexpr void
1736 assign_error(G&& error)
1737 {
1738 if (storage_)
1739 storage_->error = std::forward<G>(error);
1740 else
1741 storage_.emplace(std::in_place, std::forward<G>(error));
1742 }
1743
1744 constexpr void
1745 move_error_from(expected_void& source)
1746 {
1747 storage_.emplace(std::in_place, std::move(source.storage_->error));
1748 source.storage_.reset();
1749 }
1750
1751 storage_type storage_;
1752};
1753
1754} // namespace detail
1755
1756template <typename E>
1757class expected<void, E> : public detail::expected_void<void, E>
1758{
1759 using base = detail::expected_void<void, E>;
1760
1761public:
1762 using base::base;
1763 using base::operator=;
1764};
1765
1766template <typename E>
1767class expected<const void, E> : public detail::expected_void<const void, E>
1768{
1770
1771public:
1772 using base::base;
1773 using base::operator=;
1774};
1775
1776template <typename E>
1777class expected<volatile void, E> : public detail::expected_void<volatile void, E>
1778{
1780
1781public:
1782 using base::base;
1783 using base::operator=;
1784};
1785
1786template <typename E>
1787class expected<const volatile void, E> : public detail::expected_void<const volatile void, E>
1788{
1790
1791public:
1792 using base::base;
1793 using base::operator=;
1794};
1795
1796// Deduce both operands before checking payload equality to avoid recursive ADL.
1797template <typename T, typename E, typename T2,
1798 std::enable_if_t<!detail::is_expected_v<T2> && !detail::is_unexpected_v<T2> && !std::is_void_v<T>, int> = 0,
1799 std::enable_if_t<detail::expected_equality_comparable_v<T, T2>, int> = 0>
1800constexpr auto
1801operator==(expected<T, E> const& lhs, T2 const& rhs) -> bool
1802{
1803 if (!lhs.has_value())
1804 return false;
1805 return *lhs == rhs;
1806}
1807template <typename T, typename E, typename T2,
1808 std::enable_if_t<!detail::is_expected_v<T2> && !detail::is_unexpected_v<T2> && !std::is_void_v<T>, int> = 0,
1809 std::enable_if_t<detail::expected_equality_comparable_v<T, T2>, int> = 0>
1810constexpr auto
1811operator==(T2 const& lhs, expected<T, E> const& rhs) -> bool
1812{
1813 return rhs == lhs;
1814}
1815template <typename T, typename E, typename T2,
1816 std::enable_if_t<!detail::is_expected_v<T2> && !detail::is_unexpected_v<T2> && !std::is_void_v<T>, int> = 0,
1817 std::enable_if_t<detail::expected_equality_comparable_v<T, T2>, int> = 0>
1818constexpr auto
1819operator!=(expected<T, E> const& lhs, T2 const& rhs) -> bool
1820{
1821 return !(lhs == rhs);
1822}
1823template <typename T, typename E, typename T2,
1824 std::enable_if_t<!detail::is_expected_v<T2> && !detail::is_unexpected_v<T2> && !std::is_void_v<T>, int> = 0,
1825 std::enable_if_t<detail::expected_equality_comparable_v<T, T2>, int> = 0>
1826constexpr auto
1827operator!=(T2 const& lhs, expected<T, E> const& rhs) -> bool
1828{
1829 return !(lhs == rhs);
1830}
1831
1832template <typename T, typename E>
1833constexpr auto
1834swap(expected<T, E>& lhs, expected<T, E>& rhs) noexcept(noexcept(lhs.swap(rhs))) -> decltype(lhs.swap(rhs))
1835{
1836 lhs.swap(rhs);
1837}
1838
1839} // namespace threadschedule
1840
1841#undef THREADSCHEDULE_EXPECTED_EXCEPTION_CONSTEXPR
1842#undef THREADSCHEDULE_EXPECTED_THROW
constexpr auto has_error() const noexcept -> bool
constexpr auto or_else(F &&f) const &
constexpr auto operator=(unexpected< G > const &error) -> derived_type &
friend constexpr auto operator!=(derived_type const &lhs, unexpected< E2 > const &rhs) -> bool
constexpr expected_void(unexpect_t, Args &&... args)
constexpr void swap(derived_type &other) noexcept(std::is_nothrow_move_constructible_v< E > &&std::is_nothrow_swappable_v< E >)
friend constexpr auto operator!=(unexpected< E2 > const &lhs, derived_type const &rhs) -> bool
constexpr void emplace() noexcept
constexpr auto and_then(F &&f) &
constexpr auto transform_error(F &&f) &&
constexpr auto transform(F &&f) const &&
constexpr auto error() const &noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const &
constexpr auto transform(F &&f) const &
constexpr void operator*() const noexcept
friend constexpr auto operator==(derived_type const &lhs, expected< T2, E2 > const &rhs) -> bool
constexpr expected_void(expected< U, G > &&source)
constexpr auto operator=(unexpected< G > &&error) -> derived_type &
constexpr auto error() &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> E &&
constexpr auto error() &noexcept THREADSCHEDULE_LIFETIMEBOUND -> E &
constexpr auto and_then(F &&f) const &
constexpr auto and_then(F &&f) const &&
constexpr auto transform_error(F &&f) const &
friend constexpr auto operator!=(derived_type const &lhs, expected< T2, E2 > const &rhs) -> bool
constexpr auto transform_error(F &&f) &
constexpr auto error() const &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const &&
friend constexpr auto operator==(unexpected< E2 > const &lhs, derived_type const &rhs) -> bool
constexpr expected_void(unexpected< G > &&source)
constexpr auto and_then(F &&f) &&
constexpr expected_void(unexpect_t, std::initializer_list< U > list, Args &&... args)
constexpr expected_void(unexpected< G > const &source)
constexpr auto transform(F &&f) &
constexpr auto or_else(F &&f) const &&
constexpr auto has_value() const noexcept -> bool
constexpr void value() const &
constexpr expected_void(expected< U, G > const &source)
constexpr auto transform_error(F &&f) const &&
constexpr auto error_or(G &&fallback) const &-> E
constexpr auto transform(F &&f) &&
constexpr expected_void() noexcept=default
constexpr auto error_or(G &&fallback) &&-> E
friend class threadschedule::expected
friend constexpr auto operator==(derived_type const &lhs, unexpected< E2 > const &rhs) -> bool
constexpr auto or_else(F &&f) &
constexpr auto or_else(F &&f) &&
expected< U, E > rebind
Definition expected.hpp:793
constexpr auto error_or(G &&fallback) const &-> E
constexpr auto value() const &THREADSCHEDULE_LIFETIMEBOUND -> T const &
Definition expected.hpp:982
constexpr auto operator=(U &&value) -> expected &
Definition expected.hpp:899
constexpr auto operator->() const noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const *
Definition expected.hpp:948
constexpr expected(expected< U, G > const &source)
Definition expected.hpp:823
constexpr expected(unexpect_t, Args &&... args)
Definition expected.hpp:879
constexpr expected(expected< U, G > &&source)
Definition expected.hpp:837
constexpr expected(unexpect_t, std::initializer_list< U > list, Args &&... args)
Definition expected.hpp:886
constexpr auto error() const &noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const &
constexpr expected(unexpected< G > &&source)
Definition expected.hpp:870
constexpr auto error() const &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const &&
constexpr auto operator*() const &noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const &
Definition expected.hpp:958
constexpr auto operator*() &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> T &&
Definition expected.hpp:963
unexpected< E > unexpected_type
Definition expected.hpp:791
constexpr auto value() &THREADSCHEDULE_LIFETIMEBOUND -> U &
Definition expected.hpp:974
constexpr auto value() const &&THREADSCHEDULE_LIFETIMEBOUND -> T const &&
Definition expected.hpp:998
constexpr auto value_or(U &&fallback) const &-> T
constexpr auto operator->() noexcept THREADSCHEDULE_LIFETIMEBOUND -> T *
Definition expected.hpp:943
constexpr auto error() &noexcept THREADSCHEDULE_LIFETIMEBOUND -> E &
friend constexpr auto operator!=(unexpected< E2 > const &lhs, expected const &rhs) -> bool
constexpr auto and_then(F &&f) &&
auto operator=(expected const &) -> expected &=default
constexpr expected(std::in_place_t, std::initializer_list< U > list, Args &&... args)
Definition expected.hpp:855
constexpr auto or_else(F &&f) &&
auto operator=(expected &&) -> expected &=default
constexpr auto transform(F &&f) &
constexpr auto has_error() const noexcept -> bool
Definition expected.hpp:933
constexpr auto error_or(G &&fallback) &&-> E
constexpr auto or_else(F &&f) const &
friend constexpr auto operator==(expected const &lhs, unexpected< E2 > const &rhs) -> bool
expected(expected const &)=default
constexpr auto operator=(unexpected< G > const &error) -> expected &
Definition expected.hpp:910
constexpr auto and_then(F &&f) &
constexpr auto transform_error(F &&f) &&
expected(expected &&)=default
friend constexpr auto operator!=(expected const &lhs, unexpected< E2 > const &rhs) -> bool
constexpr auto has_value() const noexcept -> bool
Definition expected.hpp:928
friend constexpr auto operator==(expected const &lhs, expected< T2, E2 > const &rhs) -> bool
constexpr auto emplace(std::initializer_list< U > list, Args &&... args) noexcept -> T &
friend constexpr auto operator!=(expected const &lhs, expected< T2, E2 > const &rhs) -> bool
constexpr auto and_then(F &&f) const &&
friend constexpr auto operator==(unexpected< E2 > const &lhs, expected const &rhs) -> bool
constexpr auto operator*() &noexcept THREADSCHEDULE_LIFETIMEBOUND -> T &
Definition expected.hpp:953
constexpr auto error() &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> E &&
constexpr auto transform(F &&f) &&
constexpr auto transform(F &&f) const &
constexpr auto emplace(Args &&... args) noexcept -> T &
constexpr auto value_or(U &&fallback) &&-> T
constexpr void swap(expected &other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_constructible_v< E > &&std::is_nothrow_swappable_v< T > &&std::is_nothrow_swappable_v< E >)
constexpr expected(unexpected< G > const &source)
Definition expected.hpp:861
constexpr auto transform_error(F &&f) &
constexpr expected(std::in_place_t, Args &&... args)
Definition expected.hpp:849
constexpr auto or_else(F &&f) const &&
constexpr expected(U &&value)
Definition expected.hpp:810
constexpr auto operator*() const &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> T const &&
Definition expected.hpp:968
constexpr auto transform_error(F &&f) const &
constexpr auto transform(F &&f) const &&
constexpr auto value() &&THREADSCHEDULE_LIFETIMEBOUND -> T &&
Definition expected.hpp:990
constexpr auto transform_error(F &&f) const &&
constexpr auto and_then(F &&f) const &
constexpr auto or_else(F &&f) &
unexpected(unexpected const &)=default
unexpected(unexpected &&)=default
constexpr auto error() const &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const &&
Definition expected.hpp:173
constexpr auto error() const &noexcept THREADSCHEDULE_LIFETIMEBOUND -> E const &
Definition expected.hpp:163
friend constexpr void swap(unexpected &lhs, unexpected &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition expected.hpp:193
friend constexpr auto operator==(unexpected const &lhs, unexpected< G > const &rhs) -> bool
Definition expected.hpp:200
constexpr auto error() &&noexcept THREADSCHEDULE_LIFETIMEBOUND -> E &&
Definition expected.hpp:178
constexpr unexpected(std::in_place_t, Args &&... args)
Definition expected.hpp:153
auto operator=(unexpected const &) -> unexpected &=default
constexpr void swap(unexpected &other) noexcept(std::is_nothrow_swappable_v< E >)
Definition expected.hpp:184
constexpr unexpected(G &&error)
Definition expected.hpp:149
friend constexpr auto operator!=(unexpected const &lhs, unexpected< G > const &rhs) -> bool
Definition expected.hpp:206
auto operator=(unexpected &&) -> unexpected &=default
constexpr unexpected(std::in_place_t, std::initializer_list< U > list, Args &&... args)
Definition expected.hpp:158
constexpr auto error() &noexcept THREADSCHEDULE_LIFETIMEBOUND -> E &
Definition expected.hpp:168
#define THREADSCHEDULE_EXPECTED_EXCEPTION_CONSTEXPR
Definition expected.hpp:43
#define THREADSCHEDULE_EXPECTED_THROW(ex)
Definition expected.hpp:37
Optional compiler diagnostics for borrowed references and pointers.
#define THREADSCHEDULE_LIFETIMEBOUND
Definition lifetime.hpp:28
Aggregates multiple thread_registry_backend instances into a single queryable view.
constexpr bool is_unexpected_v
Definition expected.hpp:124
@ other
Standard round-robin time-sharing.
Definition native.hpp:95
std::remove_cv_t< std::remove_reference_t< T > > expected_remove_cvref_t
Definition expected.hpp:121
constexpr bool valid_expected_error_v
Definition expected.hpp:129
constexpr unexpect_t unexpect
Definition expected.hpp:53
constexpr auto swap(expected< T, E > &lhs, expected< T, E > &rhs) noexcept(noexcept(lhs.swap(rhs))) -> decltype(lhs.swap(rhs))
constexpr auto operator!=(expected< T, E > const &lhs, T2 const &rhs) -> bool
unexpected(E) -> unexpected< E >
constexpr auto operator==(expected< T, E > const &lhs, T2 const &rhs) -> bool