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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_EXPECTED_UNEXPECTED_HPP
5#define TETL_EXPECTED_UNEXPECTED_HPP
6
7#include <etl/_type_traits/is_constructible.hpp>
8#include <etl/_type_traits/is_nothrow_constructible.hpp>
9#include <etl/_type_traits/is_nothrow_swappable.hpp>
10#include <etl/_type_traits/is_same.hpp>
11#include <etl/_type_traits/is_swappable.hpp>
12#include <etl/_type_traits/remove_cvref.hpp>
13#include <etl/_utility/forward.hpp>
14#include <etl/_utility/in_place.hpp>
15#include <etl/_utility/move.hpp>
16#include <etl/_utility/swap.hpp>
17
18namespace etl {
19
20/// \ingroup expected
21template <typename E>
22struct unexpected {
23 template <typename Err = E>
24 requires(
25 not etl::is_same_v<etl::remove_cvref_t<Err>, unexpected> //
26 and not etl::is_same_v<etl::remove_cvref_t<Err>, etl::in_place_t> //
27 and etl::is_constructible_v<E, Err>
28 )
29 constexpr explicit unexpected(Err&& e) noexcept(etl::is_nothrow_constructible_v<E, Err>)
30 : _unex(etl::forward<Err>(e))
31 {
32 }
33
34 template <typename... Args>
35 requires etl::is_constructible_v<E, Args...>
36 constexpr explicit unexpected(etl::in_place_t /*tag*/, Args&&... args) noexcept(
38 )
39 : _unex(etl::forward<Args>(args)...)
40 {
41 }
42
43 constexpr unexpected(unexpected const&) = default;
44 constexpr unexpected(unexpected&&) = default;
45
46 constexpr auto operator=(unexpected const&) -> unexpected& = default;
47 constexpr auto operator=(unexpected&&) -> unexpected& = default;
48
49 [[nodiscard]] constexpr auto error() const& noexcept -> E const&
50 {
51 return _unex;
52 }
53
54 [[nodiscard]] constexpr auto error() & noexcept -> E&
55 {
56 return _unex;
57 }
58
59 [[nodiscard]] constexpr auto error() const&& noexcept -> E const&&
60 {
61 return etl::move(_unex);
62 }
63
64 [[nodiscard]] constexpr auto error() && noexcept -> E&&
65 {
66 return etl::move(_unex);
67 }
68
69 constexpr auto swap(unexpected& other) noexcept(etl::is_nothrow_swappable_v<E>) -> void
70 requires etl::is_swappable_v<E>
71 {
72 using etl::swap;
73 swap(error(), other.error());
74 }
75
76 template <typename E2>
77 friend constexpr auto operator==(unexpected const& lhs, unexpected<E2> const& rhs) -> bool
78 {
79 return lhs.error() == rhs.error();
80 }
81
82 friend constexpr auto swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))) -> void
83 requires etl::is_swappable_v<E>
84 {
85 x.swap(y);
86 }
87
88private:
89 E _unex;
90};
91
92template <typename E>
94
95} // namespace etl
96#endif // TETL_EXPECTED_UNEXPECTED_HPP
Definition adjacent_find.hpp:9
unexpected(E) -> unexpected< E >
Disambiguation tags that can be passed to the constructors of optional, variant, and any to indicate ...
Definition in_place.hpp:21
Definition unexpected.hpp:22
constexpr unexpected(unexpected &&)=default
constexpr unexpected(unexpected const &)=default
constexpr unexpected(Err &&e) noexcept(etl::is_nothrow_constructible_v< E, Err >)
Definition unexpected.hpp:29
constexpr unexpected(etl::in_place_t, Args &&... args) noexcept(etl::is_nothrow_constructible_v< E, Args... >)
Definition unexpected.hpp:36
constexpr auto error() &noexcept -> E &
Definition unexpected.hpp:54
friend constexpr auto swap(unexpected &x, unexpected &y) noexcept(noexcept(x.swap(y))) -> void
Definition unexpected.hpp:82
constexpr auto operator=(unexpected const &) -> unexpected &=default
constexpr auto error() const &noexcept -> E const &
Definition unexpected.hpp:49
constexpr auto swap(unexpected &other) noexcept(etl::is_nothrow_swappable_v< E >) -> void
Definition unexpected.hpp:69
constexpr auto error() const &&noexcept -> E const &&
Definition unexpected.hpp:59
friend constexpr auto operator==(unexpected const &lhs, unexpected< E2 > const &rhs) -> bool
Definition unexpected.hpp:77
constexpr auto error() &&noexcept -> E &&
Definition unexpected.hpp:64
constexpr auto operator=(unexpected &&) -> unexpected &=default