tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
unexpected.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_EXPECTED_UNEXPECTED_HPP
4#define TETL_EXPECTED_UNEXPECTED_HPP
5
13#include <etl/_utility/move.hpp>
14#include <etl/_utility/swap.hpp>
15
16namespace etl {
17
19template <typename E>
20struct unexpected {
21 template <typename Err = E>
25 constexpr explicit unexpected(Err&& e)
26 : _unex(etl::forward<Err>(e))
27 {
28 }
29
30 template <typename... Args>
31 requires etl::is_constructible_v<E, Args...>
32 constexpr explicit unexpected(etl::in_place_t /*tag*/, Args&&... args)
33 : _unex(etl::forward<Args>(args)...)
34 {
35 }
36
37 constexpr unexpected(unexpected const&) = default;
38 constexpr unexpected(unexpected&&) = default;
39
40 constexpr auto operator=(unexpected const&) -> unexpected& = default;
41 constexpr auto operator=(unexpected&&) -> unexpected& = default;
42
43 [[nodiscard]] constexpr auto error() const& noexcept -> E const& { return _unex; }
44
45 [[nodiscard]] constexpr auto error() & noexcept -> E& { return _unex; }
46
47 [[nodiscard]] constexpr auto error() const&& noexcept -> E const&& { return etl::move(_unex); }
48
49 [[nodiscard]] constexpr auto error() && noexcept -> E&& { return etl::move(_unex); }
50
51 constexpr auto swap(unexpected& other) noexcept(etl::is_nothrow_swappable_v<E>) -> void
52 {
53 using etl::swap;
54 swap(error(), other.error());
55 }
56
57 template <typename E2>
58 friend constexpr auto operator==(unexpected const& lhs, unexpected<E2> const& rhs) -> bool
59 {
60 return lhs.error() == rhs.error();
61 }
62
63 friend constexpr auto swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))) -> void
65 {
66 x.swap(y);
67 }
68
69private:
70 E _unex;
71};
72
73template <typename E>
75
76} // namespace etl
77#endif // TETL_EXPECTED_UNEXPECTED_HPP
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
Definition adjacent_find.hpp:8
constexpr bool is_constructible_v
Definition is_constructible.hpp:24
constexpr bool is_swappable_v
Definition is_swappable.hpp:21
unexpected(E) -> unexpected< E >
constexpr bool is_same_v
Definition is_same.hpp:11
auto swap(inplace_function< R(Args...), Capacity, Alignment > &lhs, inplace_function< R(Args...), Capacity, Alignment > &rhs) noexcept -> void
Overloads the etl::swap algorithm for etl::inplace_function. Exchanges the state of lhs with that of ...
Definition inplace_function.hpp:249
constexpr auto forward(remove_reference_t< T > &param) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18
constexpr bool is_nothrow_swappable_v
Definition is_nothrow_swappable.hpp:19
Disambiguation tags that can be passed to the constructors of optional, variant, and any to indicate ...
Definition in_place.hpp:20
Definition unexpected.hpp:20
constexpr unexpected(unexpected &&)=default
constexpr unexpected(Err &&e)
Definition unexpected.hpp:25
constexpr auto swap(unexpected &other) noexcept(etl::is_nothrow_swappable_v< E >) -> void
Definition unexpected.hpp:51
constexpr unexpected(unexpected const &)=default
constexpr auto error() &noexcept -> E &
Definition unexpected.hpp:45
constexpr auto operator=(unexpected const &) -> unexpected &=default
constexpr auto error() const &noexcept -> E const &
Definition unexpected.hpp:43
constexpr unexpected(etl::in_place_t, Args &&... args)
Definition unexpected.hpp:32
constexpr auto error() const &&noexcept -> E const &&
Definition unexpected.hpp:47
friend constexpr auto swap(unexpected &x, unexpected &y) noexcept(noexcept(x.swap(y))) -> void requires(etl::is_swappable_v< E >)
Definition unexpected.hpp:63
friend constexpr auto operator==(unexpected const &lhs, unexpected< E2 > const &rhs) -> bool
Definition unexpected.hpp:58
constexpr auto error() &&noexcept -> E &&
Definition unexpected.hpp:49
constexpr auto operator=(unexpected &&) -> unexpected &=default