VCCC  2024.05
VisualCamp Common C++ library
unexpected.hpp
Go to the documentation of this file.
1 //
2 // Created by YongGyu Lee on 3/14/24.
3 //
4 
5 #ifndef VCCC_EXPECTED_UNEXPECTED_HPP_
6 #define VCCC_EXPECTED_UNEXPECTED_HPP_
7 
8 #include <type_traits>
9 #include <utility>
10 
18 
19 namespace vccc {
20 
23 
24 template<typename E>
25 class unexpected {
26  public:
27  static_assert(std::is_object<E>::value, "Constraints not satisfied");
28  static_assert(!std::is_array<E>::value, "Constraints not satisfied");
29  static_assert(!std::is_const<E>::value, "Constraints not satisfied");
30  static_assert(!std::is_volatile<E>::value, "Constraints not satisfied");
31  static_assert(!is_specialization<E, unexpected>::value, "Constraints not satisfied");
32 
33  constexpr unexpected(const unexpected&) = default;
34  constexpr unexpected(unexpected&&) = default;
35 
36  template<typename Err = E, std::enable_if_t<conjunction<
39  std::is_constructible<E, Err>
40  >::value, int> = 0>
41  constexpr explicit unexpected(Err&& e)
42  : error_(std::forward<Err>(e)) {}
43 
44  template<typename... Args, std::enable_if_t<
45  std::is_constructible<E, Args...>
46  ::value, int> = 0>
47  constexpr explicit unexpected(in_place_t, Args&&... args)
48  : error_(std::forward<Args>(args)...) {}
49 
50  template<typename U, typename... Args, std::enable_if_t<
51  std::is_constructible<E, std::initializer_list<U>&, Args...>
52  ::value, int> = 0>
53  constexpr explicit unexpected(in_place_t, std::initializer_list<U> il, Args&&... args)
54  : error_(il, std::forward<Args>(args)...) {}
55 
56  constexpr unexpected& operator=(const unexpected&) = default;
57  constexpr unexpected& operator=(unexpected&&) = default;
58 
59  constexpr const E& error() const& noexcept {
60  return error_;
61  }
62  constexpr E& error() & noexcept {
63  return error_;
64  }
65  constexpr const E&& error() const&& noexcept {
66  return std::move(error_);
67  }
68  constexpr E&& error() && noexcept {
69  return std::move(error_);
70  }
71 
73  constexpr void swap(unexpected& other) noexcept(is_nothrow_swappable<E>::value) {
74  using std::swap;
75  swap(error(), other.error());
76  }
77 
79  friend constexpr bool operator==(unexpected& x, unexpected<E2>& y) {
80  return x.error() == y.error();
81  }
82 
84  friend constexpr bool operator!=(unexpected& x, unexpected<E2>& y) {
85  return !(x == y);
86  }
87 
88  // TODO: Implement friend swap
89 
90  private:
91  E error_;
92 };
93 
94 #if __cplusplus >= 201703L
95 
96 template<typename E>
97 unexpected(E) -> unexpected<E>;
98 
99 #endif
100 
102 
103 } // namespace vccc
104 
105 #endif // VCCC_EXPECTED_UNEXPECTED_HPP_
Definition: unexpected.hpp:25
constexpr const E && error() const &&noexcept
Definition: unexpected.hpp:65
constexpr unexpected(Err &&e)
Definition: unexpected.hpp:41
constexpr unexpected & operator=(const unexpected &)=default
constexpr unexpected(unexpected &&)=default
constexpr unexpected & operator=(unexpected &&)=default
constexpr E && error() &&noexcept
Definition: unexpected.hpp:68
constexpr friend bool operator==(unexpected &x, unexpected< E2 > &y)
Definition: unexpected.hpp:79
constexpr friend bool operator!=(unexpected &x, unexpected< E2 > &y)
Definition: unexpected.hpp:84
constexpr void swap(unexpected &other) noexcept(is_nothrow_swappable< E >::value)
Definition: unexpected.hpp:73
constexpr unexpected(in_place_t, Args &&... args)
Definition: unexpected.hpp:47
constexpr E & error() &noexcept
Definition: unexpected.hpp:62
constexpr unexpected(const unexpected &)=default
constexpr const E & error() const &noexcept
Definition: unexpected.hpp:59
constexpr unexpected(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: unexpected.hpp:53
constexpr T e
the mathematical constant
Definition: constants.hpp:37
void swap(::vccc::optional< T > &lhs, ::vccc::optional< T > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition: swap.h:30
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
Definition: conjunction.hpp:22
in-place construction tag
Definition: in_place.hpp:25
Definition: is_swappable.hpp:125
Definition: is_specialization.hpp:30
Definition: negation.hpp:23