File indexing completed on 2026-05-03 08:13:47
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___EXPECTED_UNEXPECTED_H
0010 #define _LIBCPP___EXPECTED_UNEXPECTED_H
0011
0012 #include <__config>
0013 #include <__type_traits/conjunction.h>
0014 #include <__type_traits/is_array.h>
0015 #include <__type_traits/is_const.h>
0016 #include <__type_traits/is_constructible.h>
0017 #include <__type_traits/is_nothrow_constructible.h>
0018 #include <__type_traits/is_object.h>
0019 #include <__type_traits/is_same.h>
0020 #include <__type_traits/is_swappable.h>
0021 #include <__type_traits/is_volatile.h>
0022 #include <__type_traits/negation.h>
0023 #include <__type_traits/remove_cvref.h>
0024 #include <__utility/forward.h>
0025 #include <__utility/in_place.h>
0026 #include <__utility/move.h>
0027 #include <__utility/swap.h>
0028 #include <initializer_list>
0029
0030 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0031 # pragma GCC system_header
0032 #endif
0033
0034 _LIBCPP_PUSH_MACROS
0035 #include <__undef_macros>
0036
0037 #if _LIBCPP_STD_VER >= 23
0038
0039 _LIBCPP_BEGIN_NAMESPACE_STD
0040
0041 template <class _Err>
0042 class unexpected;
0043
0044 template <class _Tp>
0045 struct __is_std_unexpected : false_type {};
0046
0047 template <class _Err>
0048 struct __is_std_unexpected<unexpected<_Err>> : true_type {};
0049
0050 template <class _Tp>
0051 using __valid_std_unexpected _LIBCPP_NODEBUG = _BoolConstant<
0052 is_object_v<_Tp> &&
0053 !is_array_v<_Tp> &&
0054 !__is_std_unexpected<_Tp>::value &&
0055 !is_const_v<_Tp> &&
0056 !is_volatile_v<_Tp>
0057 >;
0058
0059 template <class _Err>
0060 class unexpected {
0061 static_assert(__valid_std_unexpected<_Err>::value,
0062 "[expected.un.general] states a program that instantiates std::unexpected for a non-object type, an "
0063 "array type, a specialization of unexpected, or a cv-qualified type is ill-formed.");
0064
0065 public:
0066 _LIBCPP_HIDE_FROM_ABI constexpr unexpected(const unexpected&) = default;
0067 _LIBCPP_HIDE_FROM_ABI constexpr unexpected(unexpected&&) = default;
0068
0069 template <class _Error = _Err>
0070 requires(!is_same_v<remove_cvref_t<_Error>, unexpected> &&
0071 !is_same_v<remove_cvref_t<_Error>, in_place_t> &&
0072 is_constructible_v<_Err, _Error>)
0073 _LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(_Error&& __error)
0074 noexcept(is_nothrow_constructible_v<_Err, _Error>)
0075 : __unex_(std::forward<_Error>(__error)) {}
0076
0077 template <class... _Args>
0078 requires is_constructible_v<_Err, _Args...>
0079 _LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, _Args&&... __args)
0080 noexcept(is_nothrow_constructible_v<_Err, _Args...>)
0081 : __unex_(std::forward<_Args>(__args)...) {}
0082
0083 template <class _Up, class... _Args>
0084 requires is_constructible_v<_Err, initializer_list<_Up>&, _Args...>
0085 _LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
0086 noexcept(is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>)
0087 : __unex_(__il, std::forward<_Args>(__args)...) {}
0088
0089 _LIBCPP_HIDE_FROM_ABI constexpr unexpected& operator=(const unexpected&) = default;
0090 _LIBCPP_HIDE_FROM_ABI constexpr unexpected& operator=(unexpected&&) = default;
0091
0092 _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept { return __unex_; }
0093 _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept { return __unex_; }
0094 _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept { return std::move(__unex_); }
0095 _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept { return std::move(__unex_); }
0096
0097 _LIBCPP_HIDE_FROM_ABI constexpr void swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Err>) {
0098 static_assert(is_swappable_v<_Err>, "unexpected::swap requires is_swappable_v<E> to be true");
0099 using std::swap;
0100 swap(__unex_, __other.__unex_);
0101 }
0102
0103 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
0104 requires is_swappable_v<_Err>
0105 {
0106 __x.swap(__y);
0107 }
0108
0109 template <class _Err2>
0110 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const unexpected& __x, const unexpected<_Err2>& __y) {
0111 return __x.__unex_ == __y.error();
0112 }
0113
0114 private:
0115 _Err __unex_;
0116 };
0117
0118 template <class _Err>
0119 unexpected(_Err) -> unexpected<_Err>;
0120
0121 _LIBCPP_END_NAMESPACE_STD
0122
0123 #endif
0124
0125 _LIBCPP_POP_MACROS
0126
0127 #endif