Path: blob/main/contrib/llvm-project/libcxx/include/__expected/expected.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8#ifndef _LIBCPP___EXPECTED_EXPECTED_H9#define _LIBCPP___EXPECTED_EXPECTED_H1011#include <__assert>12#include <__config>13#include <__expected/bad_expected_access.h>14#include <__expected/unexpect.h>15#include <__expected/unexpected.h>16#include <__functional/invoke.h>17#include <__memory/addressof.h>18#include <__memory/construct_at.h>19#include <__type_traits/conjunction.h>20#include <__type_traits/disjunction.h>21#include <__type_traits/integral_constant.h>22#include <__type_traits/is_assignable.h>23#include <__type_traits/is_constructible.h>24#include <__type_traits/is_convertible.h>25#include <__type_traits/is_function.h>26#include <__type_traits/is_nothrow_assignable.h>27#include <__type_traits/is_nothrow_constructible.h>28#include <__type_traits/is_reference.h>29#include <__type_traits/is_same.h>30#include <__type_traits/is_swappable.h>31#include <__type_traits/is_trivially_constructible.h>32#include <__type_traits/is_trivially_destructible.h>33#include <__type_traits/is_trivially_relocatable.h>34#include <__type_traits/is_void.h>35#include <__type_traits/lazy.h>36#include <__type_traits/negation.h>37#include <__type_traits/remove_cv.h>38#include <__type_traits/remove_cvref.h>39#include <__utility/as_const.h>40#include <__utility/exception_guard.h>41#include <__utility/forward.h>42#include <__utility/in_place.h>43#include <__utility/move.h>44#include <__utility/swap.h>45#include <__verbose_abort>46#include <initializer_list>4748#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)49# pragma GCC system_header50#endif5152_LIBCPP_PUSH_MACROS53#include <__undef_macros>5455#if _LIBCPP_STD_VER >= 235657_LIBCPP_BEGIN_NAMESPACE_STD5859template <class _Tp, class _Err>60class expected;6162template <class _Tp>63struct __is_std_expected : false_type {};6465template <class _Tp, class _Err>66struct __is_std_expected<expected<_Tp, _Err>> : true_type {};6768struct __expected_construct_in_place_from_invoke_tag {};69struct __expected_construct_unexpected_from_invoke_tag {};7071template <class _Err, class _Arg>72_LIBCPP_HIDE_FROM_ABI void __throw_bad_expected_access(_Arg&& __arg) {73# ifndef _LIBCPP_HAS_NO_EXCEPTIONS74throw bad_expected_access<_Err>(std::forward<_Arg>(__arg));75# else76(void)__arg;77_LIBCPP_VERBOSE_ABORT("bad_expected_access was thrown in -fno-exceptions mode");78# endif79}8081// If parameter type `_Tp` of `__conditional_no_unique_address` is neither82// copyable nor movable, a constructor with this tag is provided. For that83// constructor, the user has to provide a function and arguments. The function84// must return an object of type `_Tp`. When the function is invoked by the85// constructor, guaranteed copy elision kicks in and the `_Tp` is constructed86// in place.87struct __conditional_no_unique_address_invoke_tag {};8889// This class implements an object with `[[no_unique_address]]` conditionally applied to it,90// based on the value of `_NoUnique`.91//92// A member of this class must always have `[[no_unique_address]]` applied to93// it. Otherwise, the `[[no_unique_address]]` in the "`_NoUnique == true`" case94// would not have any effect. In the `false` case, the `__v` is not95// `[[no_unique_address]]`, so nullifies the effects of the "outer"96// `[[no_unique_address]]` regarding data layout.97//98// If we had a language feature, this class would basically be replaced by `[[no_unique_address(condition)]]`.99template <bool _NoUnique, class _Tp>100struct __conditional_no_unique_address;101102template <class _Tp>103struct __conditional_no_unique_address<true, _Tp> {104template <class... _Args>105_LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)106: __v(std::forward<_Args>(__args)...) {}107108template <class _Func, class... _Args>109_LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(110__conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)111: __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}112113_LIBCPP_NO_UNIQUE_ADDRESS _Tp __v;114};115116template <class _Tp>117struct __conditional_no_unique_address<false, _Tp> {118template <class... _Args>119_LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(in_place_t, _Args&&... __args)120: __v(std::forward<_Args>(__args)...) {}121122template <class _Func, class... _Args>123_LIBCPP_HIDE_FROM_ABI constexpr explicit __conditional_no_unique_address(124__conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args)125: __v(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}126127_Tp __v;128};129130// This function returns whether the type `_Second` can be stuffed into the tail padding131// of the `_First` type if both of them are given `[[no_unique_address]]`.132template <class _First, class _Second>133inline constexpr bool __fits_in_tail_padding = []() {134struct __x {135_LIBCPP_NO_UNIQUE_ADDRESS _First __first;136_LIBCPP_NO_UNIQUE_ADDRESS _Second __second;137};138return sizeof(__x) == sizeof(_First);139}();140141// This class implements the storage used by `std::expected`. We have a few142// goals for this storage:143// 1. Whenever the underlying {_Tp | _Unex} combination has free bytes in its144// tail padding, we should reuse it to store the bool discriminator of the145// expected, so as to save space.146// 2. Whenever the `expected<_Tp, _Unex>` as a whole has free bytes in its tail147// padding, we should allow an object following the expected to be stored in148// its tail padding.149// 3. However, we never want a user object (say `X`) that would follow an150// `expected<_Tp, _Unex>` to be stored in the padding bytes of the151// underlying {_Tp | _Unex} union, if any. That is because we use152// `construct_at` on that union, which would end up overwriting the `X`153// member if it is stored in the tail padding of the union.154//155// To achieve this, `__expected_base`'s logic is implemented in an inner156// `__repr` class. `__expected_base` holds one `__repr` member which is157// conditionally `[[no_unique_address]]`. The `__repr` class holds the158// underlying {_Tp | _Unex} union and a boolean "has value" flag.159//160// Which one of the `__repr_`/`__union_` members is `[[no_unique_address]]`161// depends on whether the "has value" boolean fits into the tail padding of162// the underlying {_Tp | _Unex} union:163//164// - In case the "has value" bool fits into the tail padding of the union, the165// whole `__repr_` member is _not_ `[[no_unique_address]]` as it needs to be166// transparently replaced on `emplace()`/`swap()` etc.167// - In case the "has value" bool does not fit into the tail padding of the168// union, only the union member must be transparently replaced (therefore is169// _not_ `[[no_unique_address]]`) and the "has value" flag must be adjusted170// manually.171//172// This way, the member that is transparently replaced on mutating operations173// is never `[[no_unique_address]]`, satisfying the requirements from174// "[basic.life]" in the standard.175//176// Stripped away of all superfluous elements, the layout of `__expected_base`177// then looks like this:178//179// template <class Tp, class Err>180// class expected_base {181// union union_t {182// [[no_unique_address]] Tp val;183// [[no_unique_address]] Err unex;184// };185//186// static constexpr bool put_flag_in_tail = fits_in_tail_padding<union_t, bool>;187// static constexpr bool allow_reusing_expected_tail_padding = !put_flag_in_tail;188//189// struct repr {190// private:191// // If "has value" fits into the tail, this should be192// // `[[no_unique_address]]`, otherwise not.193// [[no_unique_address]] conditional_no_unique_address<194// put_flag_in_tail,195// union_t>::type union_;196// [[no_unique_address]] bool has_val_;197// };198//199// protected:200// // If "has value" fits into the tail, this must _not_ be201// // `[[no_unique_address]]` so that we fill out the202// // complete `expected` object.203// [[no_unique_address]] conditional_no_unique_address<204// allow_reusing_expected_tail_padding,205// repr>::type repr_;206// };207//208template <class _Tp, class _Err>209class __expected_base {210// use named union because [[no_unique_address]] cannot be applied to an unnamed union,211// also guaranteed elision into a potentially-overlapping subobject is unsettled (and212// it's not clear that it's implementable, given that the function is allowed to clobber213// the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.214union __union_t {215_LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;216_LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)217requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&218is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)219= default;220_LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;221_LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)222requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&223is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)224= default;225_LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;226_LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&) = delete;227228template <class... _Args>229_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t, _Args&&... __args)230: __val_(std::forward<_Args>(__args)...) {}231232template <class... _Args>233_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)234: __unex_(std::forward<_Args>(__args)...) {}235236template <class _Func, class... _Args>237_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(238std::__expected_construct_in_place_from_invoke_tag, _Func&& __f, _Args&&... __args)239: __val_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}240241template <class _Func, class... _Args>242_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(243std::__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)244: __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}245246_LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()247requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)248= default;249250// __repr's destructor handles this251_LIBCPP_HIDE_FROM_ABI constexpr ~__union_t() {}252253_LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;254_LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;255};256257static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>;258static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;259260struct __repr {261_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;262263template <class... _Args>264_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag, _Args&&... __args)265: __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}266267template <class... _Args>268_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)269: __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}270271template <class... _Args>272_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_in_place_from_invoke_tag __tag,273_Args&&... __args)274: __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(true) {}275276template <class... _Args>277_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,278_Args&&... __args)279: __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}280281// The return value of `__make_union` must be constructed in place in the282// `__v` member of `__union_`, relying on guaranteed copy elision. To do283// this, the `__conditional_no_unique_address_invoke_tag` constructor is284// called with a lambda that is immediately called inside285// `__conditional_no_unique_address`'s constructor.286template <class _OtherUnion>287_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)288requires(__allow_reusing_expected_tail_padding)289: __union_(__conditional_no_unique_address_invoke_tag{},290[&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),291__has_val_(__has_val) {}292293_LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;294_LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)295requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&296is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>)297= default;298_LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;299_LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)300requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&301is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>)302= default;303304_LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;305_LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;306307_LIBCPP_HIDE_FROM_ABI constexpr ~__repr()308requires(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>)309= default;310311_LIBCPP_HIDE_FROM_ABI constexpr ~__repr()312requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)313{314__destroy_union_member();315}316317_LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()318requires(__allow_reusing_expected_tail_padding &&319(is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Err>))320{321// Note: Since the destructor of the union is trivial, this does nothing322// except to end the lifetime of the union.323std::destroy_at(&__union_.__v);324}325326_LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()327requires(__allow_reusing_expected_tail_padding &&328(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>))329{330__destroy_union_member();331std::destroy_at(&__union_.__v);332}333334template <class... _Args>335_LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t, _Args&&... __args)336requires(__allow_reusing_expected_tail_padding)337{338std::construct_at(&__union_.__v, in_place, std::forward<_Args>(__args)...);339__has_val_ = true;340}341342template <class... _Args>343_LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)344requires(__allow_reusing_expected_tail_padding)345{346std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);347__has_val_ = false;348}349350private:351template <class, class>352friend class __expected_base;353354_LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()355requires(!is_trivially_destructible_v<_Tp> || !is_trivially_destructible_v<_Err>)356{357if (__has_val_) {358std::destroy_at(std::addressof(__union_.__v.__val_));359} else {360std::destroy_at(std::addressof(__union_.__v.__unex_));361}362}363364template <class _OtherUnion>365_LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)366requires(__allow_reusing_expected_tail_padding)367{368if (__has_val)369return __union_t(in_place, std::forward<_OtherUnion>(__other).__val_);370else371return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);372}373374_LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;375_LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;376};377378template <class _OtherUnion>379_LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)380requires(__put_flag_in_tail)381{382if (__has_val)383return __repr(in_place, std::forward<_OtherUnion>(__other).__val_);384else385return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);386}387388protected:389template <class... _Args>390_LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(_Args&&... __args)391: __repr_(in_place, std::forward<_Args>(__args)...) {}392393// In case we copy/move construct from another `expected` we need to create394// our `expected` so that it either has a value or not, depending on the "has395// value" flag of the other `expected`. To do this without falling back on396// `std::construct_at` we rely on guaranteed copy elision using two helper397// functions `__make_repr` and `__make_union`. There have to be two since398// there are two data layouts with different members being399// `[[no_unique_address]]`. GCC (as of version 13) does not do guaranteed400// copy elision when initializing `[[no_unique_address]]` members. The two401// cases are:402//403// - `__make_repr`: This is used when the "has value" flag lives in the tail404// of the union. In this case, the `__repr` member is _not_405// `[[no_unique_address]]`.406// - `__make_union`: When the "has value" flag does _not_ fit in the tail of407// the union, the `__repr` member is `[[no_unique_address]]` and the union408// is not.409//410// This constructor "catches" the first case and leaves the second case to411// `__union_t`, its constructors and `__make_union`.412template <class _OtherUnion>413_LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_base(bool __has_val, _OtherUnion&& __other)414requires(__put_flag_in_tail)415: __repr_(__conditional_no_unique_address_invoke_tag{},416[&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}417418_LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {419if constexpr (__put_flag_in_tail)420std::destroy_at(&__repr_.__v);421else422__repr_.__v.__destroy_union();423}424425template <class _Tag, class... _Args>426_LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {427if constexpr (__put_flag_in_tail)428std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);429else430__repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);431}432433_LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }434_LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }435_LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }436_LIBCPP_HIDE_FROM_ABI constexpr _Tp& __val() { return __repr_.__v.__union_.__v.__val_; }437_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& __val() const { return __repr_.__v.__union_.__v.__val_; }438_LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }439_LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }440441private:442_LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;443};444445template <class _Tp, class _Err>446class expected : private __expected_base<_Tp, _Err> {447static_assert(!is_reference_v<_Tp> && !is_function_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, in_place_t> &&448!is_same_v<remove_cv_t<_Tp>, unexpect_t> && !__is_std_unexpected<remove_cv_t<_Tp>>::value &&449__valid_std_unexpected<_Err>::value,450"[expected.object.general] A program that instantiates the definition of template expected<T, E> for a "451"reference type, a function type, or for possibly cv-qualified types in_place_t, unexpect_t, or a "452"specialization of unexpected for the T parameter is ill-formed. A program that instantiates the "453"definition of the template expected<T, E> with a type for the E parameter that is not a valid "454"template argument for unexpected is ill-formed.");455456template <class _Up, class _OtherErr>457friend class expected;458459using __base = __expected_base<_Tp, _Err>;460461public:462using value_type = _Tp;463using error_type = _Err;464using unexpected_type = unexpected<_Err>;465466using __trivially_relocatable =467__conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value && __libcpp_is_trivially_relocatable<_Err>::value,468expected,469void>;470471template <class _Up>472using rebind = expected<_Up, error_type>;473474// [expected.object.ctor], constructors475_LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept(is_nothrow_default_constructible_v<_Tp>) // strengthened476requires is_default_constructible_v<_Tp>477: __base(in_place) {}478479_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;480481_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)482requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Tp> &&483is_trivially_copy_constructible_v<_Err>)484= default;485486_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(487is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened488requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&489!(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))490: __base(__other.__has_val(), __other.__union()) {}491492_LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)493requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Tp> &&494is_trivially_move_constructible_v<_Err>)495= default;496497_LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __other) noexcept(498is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_constructible_v<_Err>)499requires(is_move_constructible_v<_Tp> && is_move_constructible_v<_Err> &&500!(is_trivially_move_constructible_v<_Tp> && is_trivially_move_constructible_v<_Err>))501: __base(__other.__has_val(), std::move(__other.__union())) {}502503private:504template <class _Up, class _OtherErr, class _UfQual, class _OtherErrQual>505using __can_convert =506_And< is_constructible<_Tp, _UfQual>,507is_constructible<_Err, _OtherErrQual>,508_If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,509_And<510_Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>, // use the copy constructor instead, see #92676511_Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,512_Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,513_Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,514_Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,515_Not<is_convertible<expected<_Up, _OtherErr>&, _Tp>>,516_Not<is_convertible<expected<_Up, _OtherErr>&&, _Tp>>,517_Not<is_convertible<const expected<_Up, _OtherErr>&, _Tp>>,518_Not<is_convertible<const expected<_Up, _OtherErr>&&, _Tp>>>,519true_type>,520_Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,521_Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,522_Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,523_Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>> >;524525template <class _Func, class... _Args>526_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(527std::__expected_construct_in_place_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)528: __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}529530template <class _Func, class... _Args>531_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(532std::__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)533: __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}534535public:536template <class _Up, class _OtherErr>537requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value538_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _Up&, _Tp> ||539!is_convertible_v<const _OtherErr&, _Err>)540expected(const expected<_Up, _OtherErr>& __other) noexcept(541is_nothrow_constructible_v<_Tp, const _Up&> &&542is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened543: __base(__other.__has_val(), __other.__union()) {}544545template <class _Up, class _OtherErr>546requires __can_convert<_Up, _OtherErr, _Up, _OtherErr>::value547_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp> || !is_convertible_v<_OtherErr, _Err>)548expected(expected<_Up, _OtherErr>&& __other) noexcept(549is_nothrow_constructible_v<_Tp, _Up> && is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened550: __base(__other.__has_val(), std::move(__other.__union())) {}551552template <class _Up = _Tp>553requires(!is_same_v<remove_cvref_t<_Up>, in_place_t> && !is_same_v<expected, remove_cvref_t<_Up>> &&554is_constructible_v<_Tp, _Up> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&555(!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_expected<remove_cvref_t<_Up>>::value))556_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_Up, _Tp>)557expected(_Up&& __u) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened558: __base(in_place, std::forward<_Up>(__u)) {}559560template <class _OtherErr>561requires is_constructible_v<_Err, const _OtherErr&>562_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(563const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened564: __base(unexpect, __unex.error()) {}565566template <class _OtherErr>567requires is_constructible_v<_Err, _OtherErr>568_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)569expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened570: __base(unexpect, std::move(__unex.error())) {}571572template <class... _Args>573requires is_constructible_v<_Tp, _Args...>574_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, _Args&&... __args) noexcept(575is_nothrow_constructible_v<_Tp, _Args...>) // strengthened576: __base(in_place, std::forward<_Args>(__args)...) {}577578template <class _Up, class... _Args>579requires is_constructible_v< _Tp, initializer_list<_Up>&, _Args... >580_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(581is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) // strengthened582: __base(in_place, __il, std::forward<_Args>(__args)...) {}583584template <class... _Args>585requires is_constructible_v<_Err, _Args...>586_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(587is_nothrow_constructible_v<_Err, _Args...>) // strengthened588: __base(unexpect, std::forward<_Args>(__args)...) {}589590template <class _Up, class... _Args>591requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >592_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(593is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened594: __base(unexpect, __il, std::forward<_Args>(__args)...) {}595596// [expected.object.dtor], destructor597598_LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;599600private:601template <class _Tag, class _OtherTag, class _T1, class _T2, class... _Args>602_LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(_T2& __oldval, _Args&&... __args) {603if constexpr (is_nothrow_constructible_v<_T1, _Args...>) {604this->__destroy();605this->__construct(_Tag{}, std::forward<_Args>(__args)...);606} else if constexpr (is_nothrow_move_constructible_v<_T1>) {607_T1 __tmp(std::forward<_Args>(__args)...);608this->__destroy();609this->__construct(_Tag{}, std::move(__tmp));610} else {611static_assert(612is_nothrow_move_constructible_v<_T2>,613"To provide strong exception guarantee, T2 has to satisfy `is_nothrow_move_constructible_v` so that it can "614"be reverted to the previous state in case an exception is thrown during the assignment.");615_T2 __tmp(std::move(__oldval));616this->__destroy();617auto __trans = std::__make_exception_guard([&] { this->__construct(_OtherTag{}, std::move(__tmp)); });618this->__construct(_Tag{}, std::forward<_Args>(__args)...);619__trans.__complete();620}621}622623public:624// [expected.object.assign], assignment625_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;626627_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(628is_nothrow_copy_assignable_v<_Tp> && is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_assignable_v<_Err> &&629is_nothrow_copy_constructible_v<_Err>) // strengthened630requires(is_copy_assignable_v<_Tp> && is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Err> &&631is_copy_constructible_v<_Err> &&632(is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))633{634if (this->__has_val() && __rhs.__has_val()) {635this->__val() = __rhs.__val();636} else if (this->__has_val()) {637__reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __rhs.__unex());638} else if (__rhs.__has_val()) {639__reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), __rhs.__val());640} else {641this->__unex() = __rhs.__unex();642}643return *this;644}645646_LIBCPP_HIDE_FROM_ABI constexpr expected&647operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Tp> && is_nothrow_move_constructible_v<_Tp> &&648is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)649requires(is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp> && is_move_constructible_v<_Err> &&650is_move_assignable_v<_Err> &&651(is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))652{653if (this->__has_val() && __rhs.__has_val()) {654this->__val() = std::move(__rhs.__val());655} else if (this->__has_val()) {656__reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__rhs.__unex()));657} else if (__rhs.__has_val()) {658__reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::move(__rhs.__val()));659} else {660this->__unex() = std::move(__rhs.__unex());661}662return *this;663}664665template <class _Up = _Tp>666_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(_Up&& __v)667requires(!is_same_v<expected, remove_cvref_t<_Up>> && !__is_std_unexpected<remove_cvref_t<_Up>>::value &&668is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up> &&669(is_nothrow_constructible_v<_Tp, _Up> || is_nothrow_move_constructible_v<_Tp> ||670is_nothrow_move_constructible_v<_Err>))671{672if (this->__has_val()) {673this->__val() = std::forward<_Up>(__v);674} else {675__reinit_expected<in_place_t, unexpect_t, _Tp, _Err>(this->__unex(), std::forward<_Up>(__v));676}677return *this;678}679680private:681template <class _OtherErrQual>682static constexpr bool __can_assign_from_unexpected =683_And< is_constructible<_Err, _OtherErrQual>,684is_assignable<_Err&, _OtherErrQual>,685_Lazy<_Or,686is_nothrow_constructible<_Err, _OtherErrQual>,687is_nothrow_move_constructible<_Tp>,688is_nothrow_move_constructible<_Err>> >::value;689690public:691template <class _OtherErr>692requires(__can_assign_from_unexpected<const _OtherErr&>)693_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {694if (this->__has_val()) {695__reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), __un.error());696} else {697this->__unex() = __un.error();698}699return *this;700}701702template <class _OtherErr>703requires(__can_assign_from_unexpected<_OtherErr>)704_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {705if (this->__has_val()) {706__reinit_expected<unexpect_t, in_place_t, _Err, _Tp>(this->__val(), std::move(__un.error()));707} else {708this->__unex() = std::move(__un.error());709}710return *this;711}712713template <class... _Args>714requires is_nothrow_constructible_v<_Tp, _Args...>715_LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(_Args&&... __args) noexcept {716this->__destroy();717this->__construct(in_place, std::forward<_Args>(__args)...);718return this->__val();719}720721template <class _Up, class... _Args>722requires is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>723_LIBCPP_HIDE_FROM_ABI constexpr _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) noexcept {724this->__destroy();725this->__construct(in_place, __il, std::forward<_Args>(__args)...);726return this->__val();727}728729public:730// [expected.object.swap], swap731_LIBCPP_HIDE_FROM_ABI constexpr void732swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp> &&733is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)734requires(is_swappable_v<_Tp> && is_swappable_v<_Err> && is_move_constructible_v<_Tp> &&735is_move_constructible_v<_Err> &&736(is_nothrow_move_constructible_v<_Tp> || is_nothrow_move_constructible_v<_Err>))737{738auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {739if constexpr (is_nothrow_move_constructible_v<_Err>) {740_Err __tmp(std::move(__with_err.__unex()));741__with_err.__destroy();742auto __trans = std::__make_exception_guard([&] { __with_err.__construct(unexpect, std::move(__tmp)); });743__with_err.__construct(in_place, std::move(__with_val.__val()));744__trans.__complete();745__with_val.__destroy();746__with_val.__construct(unexpect, std::move(__tmp));747} else {748static_assert(is_nothrow_move_constructible_v<_Tp>,749"To provide strong exception guarantee, Tp has to satisfy `is_nothrow_move_constructible_v` so "750"that it can be reverted to the previous state in case an exception is thrown during swap.");751_Tp __tmp(std::move(__with_val.__val()));752__with_val.__destroy();753auto __trans = std::__make_exception_guard([&] { __with_val.__construct(in_place, std::move(__tmp)); });754__with_val.__construct(unexpect, std::move(__with_err.__unex()));755__trans.__complete();756__with_err.__destroy();757__with_err.__construct(in_place, std::move(__tmp));758}759};760761if (this->__has_val()) {762if (__rhs.__has_val()) {763using std::swap;764swap(this->__val(), __rhs.__val());765} else {766__swap_val_unex_impl(*this, __rhs);767}768} else {769if (__rhs.__has_val()) {770__swap_val_unex_impl(__rhs, *this);771} else {772using std::swap;773swap(this->__unex(), __rhs.__unex());774}775}776}777778_LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))779requires requires { __x.swap(__y); }780{781__x.swap(__y);782}783784// [expected.object.obs], observers785_LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept {786_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(787this->__has_val(), "expected::operator-> requires the expected to contain a value");788return std::addressof(this->__val());789}790791_LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept {792_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(793this->__has_val(), "expected::operator-> requires the expected to contain a value");794return std::addressof(this->__val());795}796797_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {798_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(799this->__has_val(), "expected::operator* requires the expected to contain a value");800return this->__val();801}802803_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {804_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(805this->__has_val(), "expected::operator* requires the expected to contain a value");806return this->__val();807}808809_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {810_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(811this->__has_val(), "expected::operator* requires the expected to contain a value");812return std::move(this->__val());813}814815_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {816_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(817this->__has_val(), "expected::operator* requires the expected to contain a value");818return std::move(this->__val());819}820821_LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }822823_LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }824825_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& value() const& {826static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");827if (!this->__has_val()) {828std::__throw_bad_expected_access<_Err>(std::as_const(error()));829}830return this->__val();831}832833_LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {834static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");835if (!this->__has_val()) {836std::__throw_bad_expected_access<_Err>(std::as_const(error()));837}838return this->__val();839}840841_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& value() const&& {842static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,843"error_type has to be both copy constructible and constructible from decltype(std::move(error()))");844if (!this->__has_val()) {845std::__throw_bad_expected_access<_Err>(std::move(error()));846}847return std::move(this->__val());848}849850_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {851static_assert(is_copy_constructible_v<_Err> && is_constructible_v<_Err, decltype(std::move(error()))>,852"error_type has to be both copy constructible and constructible from decltype(std::move(error()))");853if (!this->__has_val()) {854std::__throw_bad_expected_access<_Err>(std::move(error()));855}856return std::move(this->__val());857}858859_LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {860_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(861!this->__has_val(), "expected::error requires the expected to contain an error");862return this->__unex();863}864865_LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {866_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(867!this->__has_val(), "expected::error requires the expected to contain an error");868return this->__unex();869}870871_LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {872_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(873!this->__has_val(), "expected::error requires the expected to contain an error");874return std::move(this->__unex());875}876877_LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {878_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(879!this->__has_val(), "expected::error requires the expected to contain an error");880return std::move(this->__unex());881}882883template <class _Up>884_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {885static_assert(is_copy_constructible_v<_Tp>, "value_type has to be copy constructible");886static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");887return this->__has_val() ? this->__val() : static_cast<_Tp>(std::forward<_Up>(__v));888}889890template <class _Up>891_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {892static_assert(is_move_constructible_v<_Tp>, "value_type has to be move constructible");893static_assert(is_convertible_v<_Up, _Tp>, "argument has to be convertible to value_type");894return this->__has_val() ? std::move(this->__val()) : static_cast<_Tp>(std::forward<_Up>(__v));895}896897template <class _Up = _Err>898_LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {899static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");900static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");901if (has_value())902return std::forward<_Up>(__error);903return error();904}905906template <class _Up = _Err>907_LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {908static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");909static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");910if (has_value())911return std::forward<_Up>(__error);912return std::move(error());913}914915// [expected.void.monadic], monadic916template <class _Func>917requires is_constructible_v<_Err, _Err&>918_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {919using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&>>;920static_assert(__is_std_expected<_Up>::value, "The result of f(**this) must be a specialization of std::expected");921static_assert(is_same_v<typename _Up::error_type, _Err>,922"The result of f(**this) must have the same error_type as this expected");923if (has_value()) {924return std::invoke(std::forward<_Func>(__f), this->__val());925}926return _Up(unexpect, error());927}928929template <class _Func>930requires is_constructible_v<_Err, const _Err&>931_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {932using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&>>;933static_assert(__is_std_expected<_Up>::value, "The result of f(**this) must be a specialization of std::expected");934static_assert(is_same_v<typename _Up::error_type, _Err>,935"The result of f(**this) must have the same error_type as this expected");936if (has_value()) {937return std::invoke(std::forward<_Func>(__f), this->__val());938}939return _Up(unexpect, error());940}941942template <class _Func>943requires is_constructible_v<_Err, _Err&&>944_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {945using _Up = remove_cvref_t<invoke_result_t<_Func, _Tp&&>>;946static_assert(947__is_std_expected<_Up>::value, "The result of f(std::move(**this)) must be a specialization of std::expected");948static_assert(is_same_v<typename _Up::error_type, _Err>,949"The result of f(std::move(**this)) must have the same error_type as this expected");950if (has_value()) {951return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));952}953return _Up(unexpect, std::move(error()));954}955956template <class _Func>957requires is_constructible_v<_Err, const _Err&&>958_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {959using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;960static_assert(961__is_std_expected<_Up>::value, "The result of f(std::move(**this)) must be a specialization of std::expected");962static_assert(is_same_v<typename _Up::error_type, _Err>,963"The result of f(std::move(**this)) must have the same error_type as this expected");964if (has_value()) {965return std::invoke(std::forward<_Func>(__f), std::move(this->__val()));966}967return _Up(unexpect, std::move(error()));968}969970template <class _Func>971requires is_constructible_v<_Tp, _Tp&>972_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {973using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;974static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");975static_assert(is_same_v<typename _Gp::value_type, _Tp>,976"The result of f(error()) must have the same value_type as this expected");977if (has_value()) {978return _Gp(in_place, this->__val());979}980return std::invoke(std::forward<_Func>(__f), error());981}982983template <class _Func>984requires is_constructible_v<_Tp, const _Tp&>985_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {986using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;987static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");988static_assert(is_same_v<typename _Gp::value_type, _Tp>,989"The result of f(error()) must have the same value_type as this expected");990if (has_value()) {991return _Gp(in_place, this->__val());992}993return std::invoke(std::forward<_Func>(__f), error());994}995996template <class _Func>997requires is_constructible_v<_Tp, _Tp&&>998_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {999using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;1000static_assert(1001__is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");1002static_assert(is_same_v<typename _Gp::value_type, _Tp>,1003"The result of f(std::move(error())) must have the same value_type as this expected");1004if (has_value()) {1005return _Gp(in_place, std::move(this->__val()));1006}1007return std::invoke(std::forward<_Func>(__f), std::move(error()));1008}10091010template <class _Func>1011requires is_constructible_v<_Tp, const _Tp&&>1012_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {1013using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;1014static_assert(1015__is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");1016static_assert(is_same_v<typename _Gp::value_type, _Tp>,1017"The result of f(std::move(error())) must have the same value_type as this expected");1018if (has_value()) {1019return _Gp(in_place, std::move(this->__val()));1020}1021return std::invoke(std::forward<_Func>(__f), std::move(error()));1022}10231024template <class _Func>1025requires is_constructible_v<_Err, _Err&>1026_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {1027using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;1028if (!has_value()) {1029return expected<_Up, _Err>(unexpect, error());1030}1031if constexpr (!is_void_v<_Up>) {1032return expected<_Up, _Err>(1033__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val());1034} else {1035std::invoke(std::forward<_Func>(__f), this->__val());1036return expected<_Up, _Err>();1037}1038}10391040template <class _Func>1041requires is_constructible_v<_Err, const _Err&>1042_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {1043using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;1044if (!has_value()) {1045return expected<_Up, _Err>(unexpect, error());1046}1047if constexpr (!is_void_v<_Up>) {1048return expected<_Up, _Err>(1049__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), this->__val());1050} else {1051std::invoke(std::forward<_Func>(__f), this->__val());1052return expected<_Up, _Err>();1053}1054}10551056template <class _Func>1057requires is_constructible_v<_Err, _Err&&>1058_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {1059using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;1060if (!has_value()) {1061return expected<_Up, _Err>(unexpect, std::move(error()));1062}1063if constexpr (!is_void_v<_Up>) {1064return expected<_Up, _Err>(1065__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val()));1066} else {1067std::invoke(std::forward<_Func>(__f), std::move(this->__val()));1068return expected<_Up, _Err>();1069}1070}10711072template <class _Func>1073requires is_constructible_v<_Err, const _Err&&>1074_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {1075using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>;1076if (!has_value()) {1077return expected<_Up, _Err>(unexpect, std::move(error()));1078}1079if constexpr (!is_void_v<_Up>) {1080return expected<_Up, _Err>(1081__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f), std::move(this->__val()));1082} else {1083std::invoke(std::forward<_Func>(__f), std::move(this->__val()));1084return expected<_Up, _Err>();1085}1086}10871088template <class _Func>1089requires is_constructible_v<_Tp, _Tp&>1090_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {1091using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;1092static_assert(__valid_std_unexpected<_Gp>::value,1093"The result of f(error()) must be a valid template argument for unexpected");1094if (has_value()) {1095return expected<_Tp, _Gp>(in_place, this->__val());1096}1097return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());1098}10991100template <class _Func>1101requires is_constructible_v<_Tp, const _Tp&>1102_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {1103using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;1104static_assert(__valid_std_unexpected<_Gp>::value,1105"The result of f(error()) must be a valid template argument for unexpected");1106if (has_value()) {1107return expected<_Tp, _Gp>(in_place, this->__val());1108}1109return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());1110}11111112template <class _Func>1113requires is_constructible_v<_Tp, _Tp&&>1114_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {1115using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;1116static_assert(__valid_std_unexpected<_Gp>::value,1117"The result of f(std::move(error())) must be a valid template argument for unexpected");1118if (has_value()) {1119return expected<_Tp, _Gp>(in_place, std::move(this->__val()));1120}1121return expected<_Tp, _Gp>(1122__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));1123}11241125template <class _Func>1126requires is_constructible_v<_Tp, const _Tp&&>1127_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {1128using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;1129static_assert(__valid_std_unexpected<_Gp>::value,1130"The result of f(std::move(error())) must be a valid template argument for unexpected");1131if (has_value()) {1132return expected<_Tp, _Gp>(in_place, std::move(this->__val()));1133}1134return expected<_Tp, _Gp>(1135__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));1136}11371138// [expected.object.eq], equality operators1139template <class _T2, class _E2>1140requires(!is_void_v<_T2>)1141_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) {1142if (__x.__has_val() != __y.__has_val()) {1143return false;1144} else {1145if (__x.__has_val()) {1146return __x.__val() == __y.__val();1147} else {1148return __x.__unex() == __y.__unex();1149}1150}1151}11521153template <class _T2>1154_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const _T2& __v) {1155return __x.__has_val() && static_cast<bool>(__x.__val() == __v);1156}11571158template <class _E2>1159_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e) {1160return !__x.__has_val() && static_cast<bool>(__x.__unex() == __e.error());1161}1162};11631164template <class _Err>1165class __expected_void_base {1166struct __empty_t {};1167// use named union because [[no_unique_address]] cannot be applied to an unnamed union,1168// also guaranteed elision into a potentially-overlapping subobject is unsettled (and1169// it's not clear that it's implementable, given that the function is allowed to clobber1170// the tail padding) - see https://github.com/itanium-cxx-abi/cxx-abi/issues/107.1171union __union_t {1172_LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&) = delete;1173_LIBCPP_HIDE_FROM_ABI constexpr __union_t(const __union_t&)1174requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)1175= default;1176_LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&) = delete;1177_LIBCPP_HIDE_FROM_ABI constexpr __union_t(__union_t&&)1178requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)1179= default;1180_LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(const __union_t&) = delete;1181_LIBCPP_HIDE_FROM_ABI constexpr __union_t& operator=(__union_t&&) = delete;11821183_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(in_place_t) : __empty_() {}11841185template <class... _Args>1186_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(unexpect_t, _Args&&... __args)1187: __unex_(std::forward<_Args>(__args)...) {}11881189template <class _Func, class... _Args>1190_LIBCPP_HIDE_FROM_ABI constexpr explicit __union_t(1191__expected_construct_unexpected_from_invoke_tag, _Func&& __f, _Args&&... __args)1192: __unex_(std::invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {}11931194_LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()1195requires(is_trivially_destructible_v<_Err>)1196= default;11971198// __repr's destructor handles this1199_LIBCPP_HIDE_FROM_ABI constexpr ~__union_t()1200requires(!is_trivially_destructible_v<_Err>)1201{}12021203_LIBCPP_NO_UNIQUE_ADDRESS __empty_t __empty_;1204_LIBCPP_NO_UNIQUE_ADDRESS _Err __unex_;1205};12061207static constexpr bool __put_flag_in_tail = __fits_in_tail_padding<__union_t, bool>;1208static constexpr bool __allow_reusing_expected_tail_padding = !__put_flag_in_tail;12091210struct __repr {1211_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr() = delete;12121213template <class... _Args>1214_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(in_place_t __tag) : __union_(in_place, __tag), __has_val_(true) {}12151216template <class... _Args>1217_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(unexpect_t __tag, _Args&&... __args)1218: __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}12191220template <class... _Args>1221_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(std::__expected_construct_unexpected_from_invoke_tag __tag,1222_Args&&... __args)1223: __union_(in_place, __tag, std::forward<_Args>(__args)...), __has_val_(false) {}12241225template <class _OtherUnion>1226_LIBCPP_HIDE_FROM_ABI constexpr explicit __repr(bool __has_val, _OtherUnion&& __other)1227requires(__allow_reusing_expected_tail_padding)1228: __union_(__conditional_no_unique_address_invoke_tag{},1229[&] { return __make_union(__has_val, std::forward<_OtherUnion>(__other)); }),1230__has_val_(__has_val) {}12311232_LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&) = delete;1233_LIBCPP_HIDE_FROM_ABI constexpr __repr(const __repr&)1234requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)1235= default;1236_LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&) = delete;1237_LIBCPP_HIDE_FROM_ABI constexpr __repr(__repr&&)1238requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)1239= default;12401241_LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(const __repr&) = delete;1242_LIBCPP_HIDE_FROM_ABI constexpr __repr& operator=(__repr&&) = delete;12431244_LIBCPP_HIDE_FROM_ABI constexpr ~__repr()1245requires(is_trivially_destructible_v<_Err>)1246= default;12471248_LIBCPP_HIDE_FROM_ABI constexpr ~__repr()1249requires(!is_trivially_destructible_v<_Err>)1250{1251__destroy_union_member();1252}12531254_LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()1255requires(__allow_reusing_expected_tail_padding && is_trivially_destructible_v<_Err>)1256{1257std::destroy_at(&__union_.__v);1258}12591260_LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union()1261requires(__allow_reusing_expected_tail_padding && !is_trivially_destructible_v<_Err>)1262{1263__destroy_union_member();1264std::destroy_at(&__union_.__v);1265}12661267_LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(in_place_t)1268requires(__allow_reusing_expected_tail_padding)1269{1270std::construct_at(&__union_.__v, in_place);1271__has_val_ = true;1272}12731274template <class... _Args>1275_LIBCPP_HIDE_FROM_ABI constexpr void __construct_union(unexpect_t, _Args&&... __args)1276requires(__allow_reusing_expected_tail_padding)1277{1278std::construct_at(&__union_.__v, unexpect, std::forward<_Args>(__args)...);1279__has_val_ = false;1280}12811282private:1283template <class>1284friend class __expected_void_base;12851286_LIBCPP_HIDE_FROM_ABI constexpr void __destroy_union_member()1287requires(!is_trivially_destructible_v<_Err>)1288{1289if (!__has_val_)1290std::destroy_at(std::addressof(__union_.__v.__unex_));1291}12921293template <class _OtherUnion>1294_LIBCPP_HIDE_FROM_ABI static constexpr __union_t __make_union(bool __has_val, _OtherUnion&& __other)1295requires(__allow_reusing_expected_tail_padding)1296{1297if (__has_val)1298return __union_t(in_place);1299else1300return __union_t(unexpect, std::forward<_OtherUnion>(__other).__unex_);1301}13021303_LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__put_flag_in_tail, __union_t> __union_;1304_LIBCPP_NO_UNIQUE_ADDRESS bool __has_val_;1305};13061307template <class _OtherUnion>1308_LIBCPP_HIDE_FROM_ABI static constexpr __repr __make_repr(bool __has_val, _OtherUnion&& __other)1309requires(__put_flag_in_tail)1310{1311if (__has_val)1312return __repr(in_place);1313else1314return __repr(unexpect, std::forward<_OtherUnion>(__other).__unex_);1315}13161317protected:1318template <class... _Args>1319_LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(_Args&&... __args)1320: __repr_(in_place, std::forward<_Args>(__args)...) {}13211322template <class _OtherUnion>1323_LIBCPP_HIDE_FROM_ABI constexpr explicit __expected_void_base(bool __has_val, _OtherUnion&& __other)1324requires(__put_flag_in_tail)1325: __repr_(__conditional_no_unique_address_invoke_tag{},1326[&] { return __make_repr(__has_val, std::forward<_OtherUnion>(__other)); }) {}13271328_LIBCPP_HIDE_FROM_ABI constexpr void __destroy() {1329if constexpr (__put_flag_in_tail)1330std::destroy_at(&__repr_.__v);1331else1332__repr_.__v.__destroy_union();1333}13341335template <class _Tag, class... _Args>1336_LIBCPP_HIDE_FROM_ABI constexpr void __construct(_Tag __tag, _Args&&... __args) {1337if constexpr (__put_flag_in_tail)1338std::construct_at(&__repr_.__v, __tag, std::forward<_Args>(__args)...);1339else1340__repr_.__v.__construct_union(__tag, std::forward<_Args>(__args)...);1341}13421343_LIBCPP_HIDE_FROM_ABI constexpr bool __has_val() const { return __repr_.__v.__has_val_; }1344_LIBCPP_HIDE_FROM_ABI constexpr __union_t& __union() { return __repr_.__v.__union_.__v; }1345_LIBCPP_HIDE_FROM_ABI constexpr const __union_t& __union() const { return __repr_.__v.__union_.__v; }1346_LIBCPP_HIDE_FROM_ABI constexpr _Err& __unex() { return __repr_.__v.__union_.__v.__unex_; }1347_LIBCPP_HIDE_FROM_ABI constexpr const _Err& __unex() const { return __repr_.__v.__union_.__v.__unex_; }13481349private:1350_LIBCPP_NO_UNIQUE_ADDRESS __conditional_no_unique_address<__allow_reusing_expected_tail_padding, __repr> __repr_;1351};13521353template <class _Tp, class _Err>1354requires is_void_v<_Tp>1355class expected<_Tp, _Err> : private __expected_void_base<_Err> {1356static_assert(__valid_std_unexpected<_Err>::value,1357"[expected.void.general] A program that instantiates expected<T, E> with a E that is not a "1358"valid argument for unexpected<E> is ill-formed");13591360template <class, class>1361friend class expected;13621363template <class _Up, class _OtherErr, class _OtherErrQual>1364using __can_convert =1365_And< is_void<_Up>,1366is_constructible<_Err, _OtherErrQual>,1367_Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>&>>,1368_Not<is_constructible<unexpected<_Err>, expected<_Up, _OtherErr>>>,1369_Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>&>>,1370_Not<is_constructible<unexpected<_Err>, const expected<_Up, _OtherErr>>>>;13711372using __base = __expected_void_base<_Err>;13731374public:1375using value_type = _Tp;1376using error_type = _Err;1377using unexpected_type = unexpected<_Err>;13781379template <class _Up>1380using rebind = expected<_Up, error_type>;13811382// [expected.void.ctor], constructors1383_LIBCPP_HIDE_FROM_ABI constexpr expected() noexcept : __base(in_place) {}13841385_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&) = delete;13861387_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected&)1388requires(is_copy_constructible_v<_Err> && is_trivially_copy_constructible_v<_Err>)1389= default;13901391_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __rhs) noexcept(1392is_nothrow_copy_constructible_v<_Err>) // strengthened1393requires(is_copy_constructible_v<_Err> && !is_trivially_copy_constructible_v<_Err>)1394: __base(__rhs.__has_val(), __rhs.__union()) {}13951396_LIBCPP_HIDE_FROM_ABI constexpr expected(expected&&)1397requires(is_move_constructible_v<_Err> && is_trivially_move_constructible_v<_Err>)1398= default;13991400_LIBCPP_HIDE_FROM_ABI constexpr expected(expected&& __rhs) noexcept(is_nothrow_move_constructible_v<_Err>)1401requires(is_move_constructible_v<_Err> && !is_trivially_move_constructible_v<_Err>)1402: __base(__rhs.__has_val(), std::move(__rhs.__union())) {}14031404template <class _Up, class _OtherErr>1405requires __can_convert<_Up, _OtherErr, const _OtherErr&>::value1406_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>)1407expected(const expected<_Up, _OtherErr>& __rhs) noexcept(1408is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened1409: __base(__rhs.__has_val(), __rhs.__union()) {}14101411template <class _Up, class _OtherErr>1412requires __can_convert<_Up, _OtherErr, _OtherErr>::value1413_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)1414expected(expected<_Up, _OtherErr>&& __rhs) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened1415: __base(__rhs.__has_val(), std::move(__rhs.__union())) {}14161417template <class _OtherErr>1418requires is_constructible_v<_Err, const _OtherErr&>1419_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<const _OtherErr&, _Err>) expected(1420const unexpected<_OtherErr>& __unex) noexcept(is_nothrow_constructible_v<_Err, const _OtherErr&>) // strengthened1421: __base(unexpect, __unex.error()) {}14221423template <class _OtherErr>1424requires is_constructible_v<_Err, _OtherErr>1425_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherErr, _Err>)1426expected(unexpected<_OtherErr>&& __unex) noexcept(is_nothrow_constructible_v<_Err, _OtherErr>) // strengthened1427: __base(unexpect, std::move(__unex.error())) {}14281429_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(in_place_t) noexcept : __base(in_place) {}14301431template <class... _Args>1432requires is_constructible_v<_Err, _Args...>1433_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, _Args&&... __args) noexcept(1434is_nothrow_constructible_v<_Err, _Args...>) // strengthened1435: __base(unexpect, std::forward<_Args>(__args)...) {}14361437template <class _Up, class... _Args>1438requires is_constructible_v< _Err, initializer_list<_Up>&, _Args... >1439_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(unexpect_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(1440is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened1441: __base(unexpect, __il, std::forward<_Args>(__args)...) {}14421443private:1444template <class _Func, class... _Args>1445_LIBCPP_HIDE_FROM_ABI constexpr explicit expected(1446__expected_construct_unexpected_from_invoke_tag __tag, _Func&& __f, _Args&&... __args)1447: __base(__tag, std::forward<_Func>(__f), std::forward<_Args>(__args)...) {}14481449public:1450// [expected.void.dtor], destructor14511452_LIBCPP_HIDE_FROM_ABI constexpr ~expected() = default;14531454private:1455template <class... _Args>1456_LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(unexpect_t, _Args&&... __args) {1457_LIBCPP_ASSERT_INTERNAL(this->__has_val(), "__reinit_expected(unexpect_t, ...) needs value to be set");14581459this->__destroy();1460auto __trans = std::__make_exception_guard([&] { this->__construct(in_place); });1461this->__construct(unexpect, std::forward<_Args>(__args)...);1462__trans.__complete();1463}14641465_LIBCPP_HIDE_FROM_ABI constexpr void __reinit_expected(in_place_t) {1466_LIBCPP_ASSERT_INTERNAL(!this->__has_val(), "__reinit_expected(in_place_t, ...) needs value to be unset");14671468this->__destroy();1469this->__construct(in_place);1470}14711472public:1473// [expected.void.assign], assignment1474_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected&) = delete;14751476_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs) noexcept(1477is_nothrow_copy_assignable_v<_Err> && is_nothrow_copy_constructible_v<_Err>) // strengthened1478requires(is_copy_assignable_v<_Err> && is_copy_constructible_v<_Err>)1479{1480if (this->__has_val()) {1481if (!__rhs.__has_val()) {1482__reinit_expected(unexpect, __rhs.__unex());1483}1484} else {1485if (__rhs.__has_val()) {1486__reinit_expected(in_place);1487} else {1488this->__unex() = __rhs.__unex();1489}1490}1491return *this;1492}14931494_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(expected&&) = delete;14951496_LIBCPP_HIDE_FROM_ABI constexpr expected&1497operator=(expected&& __rhs) noexcept(is_nothrow_move_assignable_v<_Err> && is_nothrow_move_constructible_v<_Err>)1498requires(is_move_assignable_v<_Err> && is_move_constructible_v<_Err>)1499{1500if (this->__has_val()) {1501if (!__rhs.__has_val()) {1502__reinit_expected(unexpect, std::move(__rhs.__unex()));1503}1504} else {1505if (__rhs.__has_val()) {1506__reinit_expected(in_place);1507} else {1508this->__unex() = std::move(__rhs.__unex());1509}1510}1511return *this;1512}15131514template <class _OtherErr>1515requires(is_constructible_v<_Err, const _OtherErr&> && is_assignable_v<_Err&, const _OtherErr&>)1516_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const unexpected<_OtherErr>& __un) {1517if (this->__has_val()) {1518__reinit_expected(unexpect, __un.error());1519} else {1520this->__unex() = __un.error();1521}1522return *this;1523}15241525template <class _OtherErr>1526requires(is_constructible_v<_Err, _OtherErr> && is_assignable_v<_Err&, _OtherErr>)1527_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(unexpected<_OtherErr>&& __un) {1528if (this->__has_val()) {1529__reinit_expected(unexpect, std::move(__un.error()));1530} else {1531this->__unex() = std::move(__un.error());1532}1533return *this;1534}15351536_LIBCPP_HIDE_FROM_ABI constexpr void emplace() noexcept {1537if (!this->__has_val()) {1538__reinit_expected(in_place);1539}1540}15411542// [expected.void.swap], swap1543_LIBCPP_HIDE_FROM_ABI constexpr void1544swap(expected& __rhs) noexcept(is_nothrow_move_constructible_v<_Err> && is_nothrow_swappable_v<_Err>)1545requires(is_swappable_v<_Err> && is_move_constructible_v<_Err>)1546{1547auto __swap_val_unex_impl = [](expected& __with_val, expected& __with_err) {1548// May throw, but will re-engage `__with_val` in that case.1549__with_val.__reinit_expected(unexpect, std::move(__with_err.__unex()));1550// Will not throw.1551__with_err.__reinit_expected(in_place);1552};15531554if (this->__has_val()) {1555if (!__rhs.__has_val()) {1556__swap_val_unex_impl(*this, __rhs);1557}1558} else {1559if (__rhs.__has_val()) {1560__swap_val_unex_impl(__rhs, *this);1561} else {1562using std::swap;1563swap(this->__unex(), __rhs.__unex());1564}1565}1566}15671568_LIBCPP_HIDE_FROM_ABI friend constexpr void swap(expected& __x, expected& __y) noexcept(noexcept(__x.swap(__y)))1569requires requires { __x.swap(__y); }1570{1571__x.swap(__y);1572}15731574// [expected.void.obs], observers1575_LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return this->__has_val(); }15761577_LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__has_val(); }15781579_LIBCPP_HIDE_FROM_ABI constexpr void operator*() const noexcept {1580_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(1581this->__has_val(), "expected::operator* requires the expected to contain a value");1582}15831584_LIBCPP_HIDE_FROM_ABI constexpr void value() const& {1585static_assert(is_copy_constructible_v<_Err>);1586if (!this->__has_val()) {1587std::__throw_bad_expected_access<_Err>(this->__unex());1588}1589}15901591_LIBCPP_HIDE_FROM_ABI constexpr void value() && {1592static_assert(is_copy_constructible_v<_Err> && is_move_constructible_v<_Err>);1593if (!this->__has_val()) {1594std::__throw_bad_expected_access<_Err>(std::move(this->__unex()));1595}1596}15971598_LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept {1599_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(1600!this->__has_val(), "expected::error requires the expected to contain an error");1601return this->__unex();1602}16031604_LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept {1605_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(1606!this->__has_val(), "expected::error requires the expected to contain an error");1607return this->__unex();1608}16091610_LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept {1611_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(1612!this->__has_val(), "expected::error requires the expected to contain an error");1613return std::move(this->__unex());1614}16151616_LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept {1617_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(1618!this->__has_val(), "expected::error requires the expected to contain an error");1619return std::move(this->__unex());1620}16211622template <class _Up = _Err>1623_LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) const& {1624static_assert(is_copy_constructible_v<_Err>, "error_type has to be copy constructible");1625static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");1626if (has_value()) {1627return std::forward<_Up>(__error);1628}1629return error();1630}16311632template <class _Up = _Err>1633_LIBCPP_HIDE_FROM_ABI constexpr _Err error_or(_Up&& __error) && {1634static_assert(is_move_constructible_v<_Err>, "error_type has to be move constructible");1635static_assert(is_convertible_v<_Up, _Err>, "argument has to be convertible to error_type");1636if (has_value()) {1637return std::forward<_Up>(__error);1638}1639return std::move(error());1640}16411642// [expected.void.monadic], monadic1643template <class _Func>1644requires is_constructible_v<_Err, _Err&>1645_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {1646using _Up = remove_cvref_t<invoke_result_t<_Func>>;1647static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");1648static_assert(1649is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");1650if (has_value()) {1651return std::invoke(std::forward<_Func>(__f));1652}1653return _Up(unexpect, error());1654}16551656template <class _Func>1657requires is_constructible_v<_Err, const _Err&>1658_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {1659using _Up = remove_cvref_t<invoke_result_t<_Func>>;1660static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");1661static_assert(1662is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");1663if (has_value()) {1664return std::invoke(std::forward<_Func>(__f));1665}1666return _Up(unexpect, error());1667}16681669template <class _Func>1670requires is_constructible_v<_Err, _Err&&>1671_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {1672using _Up = remove_cvref_t<invoke_result_t<_Func>>;1673static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");1674static_assert(1675is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");1676if (has_value()) {1677return std::invoke(std::forward<_Func>(__f));1678}1679return _Up(unexpect, std::move(error()));1680}16811682template <class _Func>1683requires is_constructible_v<_Err, const _Err&&>1684_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {1685using _Up = remove_cvref_t<invoke_result_t<_Func>>;1686static_assert(__is_std_expected<_Up>::value, "The result of f() must be a specialization of std::expected");1687static_assert(1688is_same_v<typename _Up::error_type, _Err>, "The result of f() must have the same error_type as this expected");1689if (has_value()) {1690return std::invoke(std::forward<_Func>(__f));1691}1692return _Up(unexpect, std::move(error()));1693}16941695template <class _Func>1696_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) & {1697using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&>>;1698static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");1699static_assert(is_same_v<typename _Gp::value_type, _Tp>,1700"The result of f(error()) must have the same value_type as this expected");1701if (has_value()) {1702return _Gp();1703}1704return std::invoke(std::forward<_Func>(__f), error());1705}17061707template <class _Func>1708_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const& {1709using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&>>;1710static_assert(__is_std_expected<_Gp>::value, "The result of f(error()) must be a specialization of std::expected");1711static_assert(is_same_v<typename _Gp::value_type, _Tp>,1712"The result of f(error()) must have the same value_type as this expected");1713if (has_value()) {1714return _Gp();1715}1716return std::invoke(std::forward<_Func>(__f), error());1717}17181719template <class _Func>1720_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) && {1721using _Gp = remove_cvref_t<invoke_result_t<_Func, _Err&&>>;1722static_assert(1723__is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");1724static_assert(is_same_v<typename _Gp::value_type, _Tp>,1725"The result of f(std::move(error())) must have the same value_type as this expected");1726if (has_value()) {1727return _Gp();1728}1729return std::invoke(std::forward<_Func>(__f), std::move(error()));1730}17311732template <class _Func>1733_LIBCPP_HIDE_FROM_ABI constexpr auto or_else(_Func&& __f) const&& {1734using _Gp = remove_cvref_t<invoke_result_t<_Func, const _Err&&>>;1735static_assert(1736__is_std_expected<_Gp>::value, "The result of f(std::move(error())) must be a specialization of std::expected");1737static_assert(is_same_v<typename _Gp::value_type, _Tp>,1738"The result of f(std::move(error())) must have the same value_type as this expected");1739if (has_value()) {1740return _Gp();1741}1742return std::invoke(std::forward<_Func>(__f), std::move(error()));1743}17441745template <class _Func>1746requires is_constructible_v<_Err, _Err&>1747_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {1748using _Up = remove_cv_t<invoke_result_t<_Func>>;1749if (!has_value()) {1750return expected<_Up, _Err>(unexpect, error());1751}1752if constexpr (!is_void_v<_Up>) {1753return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));1754} else {1755std::invoke(std::forward<_Func>(__f));1756return expected<_Up, _Err>();1757}1758}17591760template <class _Func>1761requires is_constructible_v<_Err, const _Err&>1762_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {1763using _Up = remove_cv_t<invoke_result_t<_Func>>;1764if (!has_value()) {1765return expected<_Up, _Err>(unexpect, error());1766}1767if constexpr (!is_void_v<_Up>) {1768return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));1769} else {1770std::invoke(std::forward<_Func>(__f));1771return expected<_Up, _Err>();1772}1773}17741775template <class _Func>1776requires is_constructible_v<_Err, _Err&&>1777_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {1778using _Up = remove_cv_t<invoke_result_t<_Func>>;1779if (!has_value()) {1780return expected<_Up, _Err>(unexpect, std::move(error()));1781}1782if constexpr (!is_void_v<_Up>) {1783return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));1784} else {1785std::invoke(std::forward<_Func>(__f));1786return expected<_Up, _Err>();1787}1788}17891790template <class _Func>1791requires is_constructible_v<_Err, const _Err&&>1792_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {1793using _Up = remove_cv_t<invoke_result_t<_Func>>;1794if (!has_value()) {1795return expected<_Up, _Err>(unexpect, std::move(error()));1796}1797if constexpr (!is_void_v<_Up>) {1798return expected<_Up, _Err>(__expected_construct_in_place_from_invoke_tag{}, std::forward<_Func>(__f));1799} else {1800std::invoke(std::forward<_Func>(__f));1801return expected<_Up, _Err>();1802}1803}18041805template <class _Func>1806_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) & {1807using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&>>;1808static_assert(__valid_std_unexpected<_Gp>::value,1809"The result of f(error()) must be a valid template argument for unexpected");1810if (has_value()) {1811return expected<_Tp, _Gp>();1812}1813return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());1814}18151816template <class _Func>1817_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const& {1818using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&>>;1819static_assert(__valid_std_unexpected<_Gp>::value,1820"The result of f(error()) must be a valid template argument for unexpected");1821if (has_value()) {1822return expected<_Tp, _Gp>();1823}1824return expected<_Tp, _Gp>(__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), error());1825}18261827template <class _Func>1828_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) && {1829using _Gp = remove_cv_t<invoke_result_t<_Func, _Err&&>>;1830static_assert(__valid_std_unexpected<_Gp>::value,1831"The result of f(std::move(error())) must be a valid template argument for unexpected");1832if (has_value()) {1833return expected<_Tp, _Gp>();1834}1835return expected<_Tp, _Gp>(1836__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));1837}18381839template <class _Func>1840_LIBCPP_HIDE_FROM_ABI constexpr auto transform_error(_Func&& __f) const&& {1841using _Gp = remove_cv_t<invoke_result_t<_Func, const _Err&&>>;1842static_assert(__valid_std_unexpected<_Gp>::value,1843"The result of f(std::move(error())) must be a valid template argument for unexpected");1844if (has_value()) {1845return expected<_Tp, _Gp>();1846}1847return expected<_Tp, _Gp>(1848__expected_construct_unexpected_from_invoke_tag{}, std::forward<_Func>(__f), std::move(error()));1849}18501851// [expected.void.eq], equality operators1852template <class _T2, class _E2>1853requires is_void_v<_T2>1854_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) {1855if (__x.__has_val() != __y.__has_val()) {1856return false;1857} else {1858return __x.__has_val() || static_cast<bool>(__x.__unex() == __y.__unex());1859}1860}18611862template <class _E2>1863_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y) {1864return !__x.__has_val() && static_cast<bool>(__x.__unex() == __y.error());1865}1866};18671868_LIBCPP_END_NAMESPACE_STD18691870#endif // _LIBCPP_STD_VER >= 2318711872_LIBCPP_POP_MACROS18731874#endif // _LIBCPP___EXPECTED_EXPECTED_H187518761877