unexpected.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___EXPECTED_UNEXPECTED_H
  10. #define _LIBCPP___EXPECTED_UNEXPECTED_H
  11. #include <__config>
  12. #include <__type_traits/conjunction.h>
  13. #include <__type_traits/is_array.h>
  14. #include <__type_traits/is_const.h>
  15. #include <__type_traits/is_constructible.h>
  16. #include <__type_traits/is_nothrow_constructible.h>
  17. #include <__type_traits/is_object.h>
  18. #include <__type_traits/is_same.h>
  19. #include <__type_traits/is_swappable.h>
  20. #include <__type_traits/is_volatile.h>
  21. #include <__type_traits/negation.h>
  22. #include <__type_traits/remove_cvref.h>
  23. #include <__utility/forward.h>
  24. #include <__utility/in_place.h>
  25. #include <__utility/move.h>
  26. #include <__utility/swap.h>
  27. #include <initializer_list>
  28. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  29. # pragma GCC system_header
  30. #endif
  31. _LIBCPP_PUSH_MACROS
  32. #include <__undef_macros>
  33. #if _LIBCPP_STD_VER >= 20
  34. _LIBCPP_BEGIN_NAMESPACE_STD
  35. template <class _Err>
  36. class unexpected;
  37. template <class _Tp>
  38. struct __is_std_unexpected : false_type {};
  39. template <class _Err>
  40. struct __is_std_unexpected<unexpected<_Err>> : true_type {};
  41. template <class _Tp>
  42. using __valid_std_unexpected = _BoolConstant< //
  43. is_object_v<_Tp> && //
  44. !is_array_v<_Tp> && //
  45. !__is_std_unexpected<_Tp>::value && //
  46. !is_const_v<_Tp> && //
  47. !is_volatile_v<_Tp> //
  48. >;
  49. template <class _Err>
  50. class unexpected {
  51. static_assert(__valid_std_unexpected<_Err>::value,
  52. "[expected.un.general] states a program that instantiates std::unexpected for a non-object type, an "
  53. "array type, a specialization of unexpected, or a cv-qualified type is ill-formed.");
  54. public:
  55. _LIBCPP_HIDE_FROM_ABI constexpr unexpected(const unexpected&) = default;
  56. _LIBCPP_HIDE_FROM_ABI constexpr unexpected(unexpected&&) = default;
  57. template <class _Error = _Err>
  58. requires(!is_same_v<remove_cvref_t<_Error>, unexpected> && //
  59. !is_same_v<remove_cvref_t<_Error>, in_place_t> && //
  60. is_constructible_v<_Err, _Error>)
  61. _LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(_Error&& __error) //
  62. noexcept(is_nothrow_constructible_v<_Err, _Error>) // strengthened
  63. : __unex_(std::forward<_Error>(__error)) {}
  64. template <class... _Args>
  65. requires is_constructible_v<_Err, _Args...>
  66. _LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, _Args&&... __args) //
  67. noexcept(is_nothrow_constructible_v<_Err, _Args...>) // strengthened
  68. : __unex_(std::forward<_Args>(__args)...) {}
  69. template <class _Up, class... _Args>
  70. requires is_constructible_v<_Err, initializer_list<_Up>&, _Args...>
  71. _LIBCPP_HIDE_FROM_ABI constexpr explicit unexpected(in_place_t, initializer_list<_Up> __il, _Args&&... __args) //
  72. noexcept(is_nothrow_constructible_v<_Err, initializer_list<_Up>&, _Args...>) // strengthened
  73. : __unex_(__il, std::forward<_Args>(__args)...) {}
  74. _LIBCPP_HIDE_FROM_ABI constexpr unexpected& operator=(const unexpected&) = default;
  75. _LIBCPP_HIDE_FROM_ABI constexpr unexpected& operator=(unexpected&&) = default;
  76. _LIBCPP_HIDE_FROM_ABI constexpr const _Err& error() const& noexcept { return __unex_; }
  77. _LIBCPP_HIDE_FROM_ABI constexpr _Err& error() & noexcept { return __unex_; }
  78. _LIBCPP_HIDE_FROM_ABI constexpr const _Err&& error() const&& noexcept { return std::move(__unex_); }
  79. _LIBCPP_HIDE_FROM_ABI constexpr _Err&& error() && noexcept { return std::move(__unex_); }
  80. _LIBCPP_HIDE_FROM_ABI constexpr void swap(unexpected& __other) noexcept(is_nothrow_swappable_v<_Err>) {
  81. static_assert(is_swappable_v<_Err>, "unexpected::swap requires is_swappable_v<E> to be true");
  82. using std::swap;
  83. swap(__unex_, __other.__unex_);
  84. }
  85. _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(unexpected& __x, unexpected& __y) noexcept(noexcept(__x.swap(__y)))
  86. requires is_swappable_v<_Err>
  87. {
  88. __x.swap(__y);
  89. }
  90. template <class _Err2>
  91. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const unexpected& __x, const unexpected<_Err2>& __y) {
  92. return __x.__unex_ == __y.__unex_;
  93. }
  94. private:
  95. _Err __unex_;
  96. };
  97. template <class _Err>
  98. unexpected(_Err) -> unexpected<_Err>;
  99. _LIBCPP_END_NAMESPACE_STD
  100. #endif // _LIBCPP_STD_VER >= 23
  101. _LIBCPP_POP_MACROS
  102. #endif // _LIBCPP___EXPECTED_UNEXPECTED_H