Skip to content

Commit

Permalink
add unexpected_type::error(), deprecate unexpected_type::value() (#66,…
Browse files Browse the repository at this point in the history
… thanks @psyinf)
  • Loading branch information
martinmoene committed Jun 4, 2024
1 parent 43d9b4a commit e4f3808
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 62 deletions.
70 changes: 52 additions & 18 deletions include/nonstd/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ class unexpected_type
)
>
constexpr explicit unexpected_type( unexpected_type<E2> const & error )
: m_error( E{ error.value() } )
: m_error( E{ error.error() } )
{}

template< typename E2
Expand All @@ -1270,7 +1270,7 @@ class unexpected_type
)
>
constexpr /*non-explicit*/ unexpected_type( unexpected_type<E2> const & error )
: m_error( error.value() )
: m_error( error.error() )
{}

template< typename E2
Expand All @@ -1288,7 +1288,7 @@ class unexpected_type
)
>
constexpr explicit unexpected_type( unexpected_type<E2> && error )
: m_error( E{ std::move( error.value() ) } )
: m_error( E{ std::move( error.error() ) } )
{}

template< typename E2
Expand All @@ -1306,7 +1306,7 @@ class unexpected_type
)
>
constexpr /*non-explicit*/ unexpected_type( unexpected_type<E2> && error )
: m_error( std::move( error.value() ) )
: m_error( std::move( error.error() ) )
{}

// x.x.5.2.2 Assignment
Expand All @@ -1317,36 +1317,70 @@ class unexpected_type
template< typename E2 = E >
nsel_constexpr14 unexpected_type & operator=( unexpected_type<E2> const & other )
{
unexpected_type{ other.value() }.swap( *this );
unexpected_type{ other.error() }.swap( *this );
return *this;
}

template< typename E2 = E >
nsel_constexpr14 unexpected_type & operator=( unexpected_type<E2> && other )
{
unexpected_type{ std::move( other.value() ) }.swap( *this );
unexpected_type{ std::move( other.error() ) }.swap( *this );
return *this;
}

// x.x.5.2.3 Observers

nsel_constexpr14 E & error() & noexcept
{
return m_error;
}

constexpr E const & error() const & noexcept
{
return m_error;
}

#if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490

nsel_constexpr14 E && error() && noexcept
{
return std::move( m_error );
}

constexpr E const && error() const && noexcept
{
return std::move( m_error );
}

#endif

// x.x.5.2.3 Observers - deprecated

nsel_deprecated("replace value() with error()")

nsel_constexpr14 E & value() & noexcept
{
return m_error;
}

nsel_deprecated("replace value() with error()")

constexpr E const & value() const & noexcept
{
return m_error;
}

#if !nsel_COMPILER_GNUC_VERSION || nsel_COMPILER_GNUC_VERSION >= 490

nsel_deprecated("replace value() with error()")

nsel_constexpr14 E && value() && noexcept
{
return std::move( m_error );
}

nsel_deprecated("replace value() with error()")

constexpr E const && value() const && noexcept
{
return std::move( m_error );
Expand Down Expand Up @@ -1435,7 +1469,7 @@ class unexpected_type< std::exception_ptr >
template< typename E1, typename E2 >
constexpr bool operator==( unexpected_type<E1> const & x, unexpected_type<E2> const & y )
{
return x.value() == y.value();
return x.error() == y.error();
}

template< typename E1, typename E2 >
Expand All @@ -1449,7 +1483,7 @@ constexpr bool operator!=( unexpected_type<E1> const & x, unexpected_type<E2> co
template< typename E >
constexpr bool operator<( unexpected_type<E> const & x, unexpected_type<E> const & y )
{
return x.value() < y.value();
return x.error() < y.error();
}

template< typename E >
Expand Down Expand Up @@ -1906,7 +1940,7 @@ class expected
nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> const & error )
: contained( false )
{
contained.construct_error( E{ error.value() } );
contained.construct_error( E{ error.error() } );
}

template< typename G = E
Expand All @@ -1918,7 +1952,7 @@ class expected
nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> const & error )
: contained( false )
{
contained.construct_error( error.value() );
contained.construct_error( error.error() );
}

template< typename G = E
Expand All @@ -1930,7 +1964,7 @@ class expected
nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> && error )
: contained( false )
{
contained.construct_error( E{ std::move( error.value() ) } );
contained.construct_error( E{ std::move( error.error() ) } );
}

template< typename G = E
Expand All @@ -1942,7 +1976,7 @@ class expected
nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> && error )
: contained( false )
{
contained.construct_error( std::move( error.value() ) );
contained.construct_error( std::move( error.error() ) );
}

// in-place construction, value
Expand Down Expand Up @@ -2047,7 +2081,7 @@ class expected
>
expected & operator=( nonstd::unexpected_type<G> const & error )
{
expected( unexpect, error.value() ).swap( *this );
expected( unexpect, error.error() ).swap( *this );
return *this;
}

Expand All @@ -2060,7 +2094,7 @@ class expected
>
expected & operator=( nonstd::unexpected_type<G> && error )
{
expected( unexpect, std::move( error.value() ) ).swap( *this );
expected( unexpect, std::move( error.error() ) ).swap( *this );
return *this;
}

Expand Down Expand Up @@ -2646,7 +2680,7 @@ class expected<void, E>
nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> const & error )
: contained( false )
{
contained.construct_error( E{ error.value() } );
contained.construct_error( E{ error.error() } );
}

template< typename G = E
Expand All @@ -2657,7 +2691,7 @@ class expected<void, E>
nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> const & error )
: contained( false )
{
contained.construct_error( error.value() );
contained.construct_error( error.error() );
}

template< typename G = E
Expand All @@ -2668,7 +2702,7 @@ class expected<void, E>
nsel_constexpr14 explicit expected( nonstd::unexpected_type<G> && error )
: contained( false )
{
contained.construct_error( E{ std::move( error.value() ) } );
contained.construct_error( E{ std::move( error.error() ) } );
}

template< typename G = E
Expand All @@ -2679,7 +2713,7 @@ class expected<void, E>
nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type<G> && error )
: contained( false )
{
contained.construct_error( std::move( error.value() ) );
contained.construct_error( std::move( error.error() ) );
}

template< typename... Args
Expand Down
Loading

0 comments on commit e4f3808

Please sign in to comment.