From e4f3808c970ca2860aba5d10086b0c8e649f1fa5 Mon Sep 17 00:00:00 2001 From: Martin Moene Date: Tue, 4 Jun 2024 16:10:40 +0200 Subject: [PATCH] add unexpected_type::error(), deprecate unexpected_type::value() (#66, thanks @psyinf) --- include/nonstd/expected.hpp | 70 +++++++++++++++++++++-------- test/expected.t.cpp | 88 ++++++++++++++++++------------------- 2 files changed, 96 insertions(+), 62 deletions(-) diff --git a/include/nonstd/expected.hpp b/include/nonstd/expected.hpp index 2c17bbb..69da5d3 100644 --- a/include/nonstd/expected.hpp +++ b/include/nonstd/expected.hpp @@ -1252,7 +1252,7 @@ class unexpected_type ) > constexpr explicit unexpected_type( unexpected_type const & error ) - : m_error( E{ error.value() } ) + : m_error( E{ error.error() } ) {} template< typename E2 @@ -1270,7 +1270,7 @@ class unexpected_type ) > constexpr /*non-explicit*/ unexpected_type( unexpected_type const & error ) - : m_error( error.value() ) + : m_error( error.error() ) {} template< typename E2 @@ -1288,7 +1288,7 @@ class unexpected_type ) > constexpr explicit unexpected_type( unexpected_type && error ) - : m_error( E{ std::move( error.value() ) } ) + : m_error( E{ std::move( error.error() ) } ) {} template< typename E2 @@ -1306,7 +1306,7 @@ class unexpected_type ) > constexpr /*non-explicit*/ unexpected_type( unexpected_type && error ) - : m_error( std::move( error.value() ) ) + : m_error( std::move( error.error() ) ) {} // x.x.5.2.2 Assignment @@ -1317,24 +1317,54 @@ class unexpected_type template< typename E2 = E > nsel_constexpr14 unexpected_type & operator=( unexpected_type 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 && 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; @@ -1342,11 +1372,15 @@ class unexpected_type #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 ); @@ -1435,7 +1469,7 @@ class unexpected_type< std::exception_ptr > template< typename E1, typename E2 > constexpr bool operator==( unexpected_type const & x, unexpected_type const & y ) { - return x.value() == y.value(); + return x.error() == y.error(); } template< typename E1, typename E2 > @@ -1449,7 +1483,7 @@ constexpr bool operator!=( unexpected_type const & x, unexpected_type co template< typename E > constexpr bool operator<( unexpected_type const & x, unexpected_type const & y ) { - return x.value() < y.value(); + return x.error() < y.error(); } template< typename E > @@ -1906,7 +1940,7 @@ class expected nsel_constexpr14 explicit expected( nonstd::unexpected_type const & error ) : contained( false ) { - contained.construct_error( E{ error.value() } ); + contained.construct_error( E{ error.error() } ); } template< typename G = E @@ -1918,7 +1952,7 @@ class expected nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type const & error ) : contained( false ) { - contained.construct_error( error.value() ); + contained.construct_error( error.error() ); } template< typename G = E @@ -1930,7 +1964,7 @@ class expected nsel_constexpr14 explicit expected( nonstd::unexpected_type && error ) : contained( false ) { - contained.construct_error( E{ std::move( error.value() ) } ); + contained.construct_error( E{ std::move( error.error() ) } ); } template< typename G = E @@ -1942,7 +1976,7 @@ class expected nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type && error ) : contained( false ) { - contained.construct_error( std::move( error.value() ) ); + contained.construct_error( std::move( error.error() ) ); } // in-place construction, value @@ -2047,7 +2081,7 @@ class expected > expected & operator=( nonstd::unexpected_type const & error ) { - expected( unexpect, error.value() ).swap( *this ); + expected( unexpect, error.error() ).swap( *this ); return *this; } @@ -2060,7 +2094,7 @@ class expected > expected & operator=( nonstd::unexpected_type && error ) { - expected( unexpect, std::move( error.value() ) ).swap( *this ); + expected( unexpect, std::move( error.error() ) ).swap( *this ); return *this; } @@ -2646,7 +2680,7 @@ class expected nsel_constexpr14 explicit expected( nonstd::unexpected_type const & error ) : contained( false ) { - contained.construct_error( E{ error.value() } ); + contained.construct_error( E{ error.error() } ); } template< typename G = E @@ -2657,7 +2691,7 @@ class expected nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type const & error ) : contained( false ) { - contained.construct_error( error.value() ); + contained.construct_error( error.error() ); } template< typename G = E @@ -2668,7 +2702,7 @@ class expected nsel_constexpr14 explicit expected( nonstd::unexpected_type && error ) : contained( false ) { - contained.construct_error( E{ std::move( error.value() ) } ); + contained.construct_error( E{ std::move( error.error() ) } ); } template< typename G = E @@ -2679,7 +2713,7 @@ class expected nsel_constexpr14 /*non-explicit*/ expected( nonstd::unexpected_type && error ) : contained( false ) { - contained.construct_error( std::move( error.value() ) ); + contained.construct_error( std::move( error.error() ) ); } template< typename... Args diff --git a/test/expected.t.cpp b/test/expected.t.cpp index 61fbd6f..5163b0e 100644 --- a/test/expected.t.cpp +++ b/test/expected.t.cpp @@ -191,8 +191,8 @@ CASE( "unexpected_type: Allows to copy-construct from unexpected_type, default" unexpected_type b( a ); - EXPECT( a.value() == 7 ); - EXPECT( b.value() == 7 ); + EXPECT( a.error() == 7 ); + EXPECT( b.error() == 7 ); } CASE( "unexpected_type: Allows to move-construct from unexpected_type, default" ) @@ -201,8 +201,8 @@ CASE( "unexpected_type: Allows to move-construct from unexpected_type, default" unexpected_type b( std::move( a ) ); - EXPECT( a.value() == 7 ); - EXPECT( b.value() == 7 ); + EXPECT( a.error() == 7 ); + EXPECT( b.error() == 7 ); } CASE( "unexpected_type: Allows to in-place-construct" ) @@ -210,32 +210,32 @@ CASE( "unexpected_type: Allows to in-place-construct" ) unexpected_type ue( in_place, 5 ); unexpected_type ui( in_place, 7 ); - EXPECT( ue.value() == Explicit{5} ); - EXPECT( ui.value() == Implicit{7} ); + EXPECT( ue.error() == Explicit{5} ); + EXPECT( ui.error() == Implicit{7} ); } CASE( "unexpected_type: Allows to in-place-construct from initializer_list" ) { unexpected_type u( in_place, { 7, 8, 9 }, 'a' ); - EXPECT( u.value().vec[0] == 7 ); - EXPECT( u.value().vec[1] == 8 ); - EXPECT( u.value().vec[2] == 9 ); - EXPECT( u.value().c == 'a'); + EXPECT( u.error().vec[0] == 7 ); + EXPECT( u.error().vec[1] == 8 ); + EXPECT( u.error().vec[2] == 9 ); + EXPECT( u.error().c == 'a'); } CASE( "unexpected_type: Allows to copy-construct from error_type" ) { unexpected_type u{ 7 }; - EXPECT( u.value() == 7 ); + EXPECT( u.error() == 7 ); } CASE( "unexpected_type: Allows to move-construct from error_type" ) { unexpected_type u{ std::move( 7 ) }; - EXPECT( u.value() == 7 ); + EXPECT( u.error() == 7 ); } CASE( "unexpected_type: Allows to copy-construct from unexpected_type, explicit converting" ) @@ -244,7 +244,7 @@ CASE( "unexpected_type: Allows to copy-construct from unexpected_type, explicit unexpected_type b{ a }; - EXPECT( b.value() == Explicit{7} ); + EXPECT( b.error() == Explicit{7} ); } CASE( "unexpected_type: Allows to copy-construct from unexpected_type, non-explicit converting" ) @@ -253,7 +253,7 @@ CASE( "unexpected_type: Allows to copy-construct from unexpected_type, non-expli unexpected_type b( a ); - EXPECT( b.value() == Implicit{7} ); + EXPECT( b.error() == Implicit{7} ); } CASE( "unexpected_type: Allows to move-construct from unexpected_type, explicit converting" ) @@ -262,7 +262,7 @@ CASE( "unexpected_type: Allows to move-construct from unexpected_type, explicit unexpected_type b{ std::move( a ) }; - EXPECT( b.value() == Explicit{7} ); + EXPECT( b.error() == Explicit{7} ); } CASE( "unexpected_type: Allows to move-construct from unexpected_type, non-explicit converting" ) @@ -271,7 +271,7 @@ CASE( "unexpected_type: Allows to move-construct from unexpected_type, non-expli unexpected_type b( std::move( a ) ); - EXPECT( b.value() == Implicit{7} ); + EXPECT( b.error() == Implicit{7} ); } CASE( "unexpected_type: Allows to copy-assign from unexpected_type, default" ) @@ -281,7 +281,7 @@ CASE( "unexpected_type: Allows to copy-assign from unexpected_type, default" ) b = a; - EXPECT( b.value() == 7 ); + EXPECT( b.error() == 7 ); } CASE( "unexpected_type: Allows to move-assign from unexpected_type, default" ) @@ -291,7 +291,7 @@ CASE( "unexpected_type: Allows to move-assign from unexpected_type, default" ) b = std::move( a ); - EXPECT( b.value() == 7 ); + EXPECT( b.error() == 7 ); } CASE( "unexpected_type: Allows to copy-assign from unexpected_type, converting" ) @@ -303,8 +303,8 @@ CASE( "unexpected_type: Allows to copy-assign from unexpected_type, converting" ue = u; ui = u; - EXPECT( ue.value() == Explicit{7} ); - EXPECT( ui.value() == Implicit{7} ); + EXPECT( ue.error() == Explicit{7} ); + EXPECT( ui.error() == Implicit{7} ); } CASE( "unexpected_type: Allows to move-assign from unexpected, converting" ) @@ -317,8 +317,8 @@ CASE( "unexpected_type: Allows to move-assign from unexpected, converting" ) ue = std::move( u ); ui = std::move( v ); - EXPECT( ue.value() == Explicit{7} ); - EXPECT( ui.value() == Implicit{7} ); + EXPECT( ue.error() == Explicit{7} ); + EXPECT( ui.error() == Implicit{7} ); } CASE( "unexpected_type: Allows to observe its value via a l-value reference" ) @@ -326,8 +326,8 @@ CASE( "unexpected_type: Allows to observe its value via a l-value reference" ) unexpected_type u{ 7 }; unexpected_type uc{ 7 }; - EXPECT( u.value() == 7 ); - EXPECT( uc.value() == 7 ); + EXPECT( u.error() == 7 ); + EXPECT( uc.error() == 7 ); } CASE( "unexpected_type: Allows to observe its value via a r-value reference" ) @@ -335,17 +335,17 @@ CASE( "unexpected_type: Allows to observe its value via a r-value reference" ) unexpected_type u{ 7 }; unexpected_type uc{ 7 }; - EXPECT( std::move( u).value() == 7 ); - EXPECT( std::move(uc).value() == 7 ); + EXPECT( std::move( u).error() == 7 ); + EXPECT( std::move(uc).error() == 7 ); } CASE( "unexpected_type: Allows to modify its value via a l-value reference" ) { unexpected_type u{ 5 }; - u.value() = 7; + u.error() = 7; - EXPECT( u.value() == 7 ); + EXPECT( u.error() == 7 ); } //CASE( "unexpected_type: Allows to modify its value via a r-value reference" ) @@ -353,9 +353,9 @@ CASE( "unexpected_type: Allows to modify its value via a l-value reference" ) // const auto v = 9; // unexpected_type u{ 7 }; // -// std::move( u.value() ) = v; +// std::move( u.error() ) = v; // -// EXPECT( u.value() == v ); +// EXPECT( u.error() == v ); //} CASE( "unexpected_type: Allows to be swapped" ) @@ -365,8 +365,8 @@ CASE( "unexpected_type: Allows to be swapped" ) a.swap( b ); - EXPECT( a.value() == 7 ); - EXPECT( b.value() == 5 ); + EXPECT( a.error() == 7 ); + EXPECT( b.error() == 5 ); } //CASE( "unexpected_type: Allows reset via = {}" ) @@ -412,7 +412,7 @@ CASE( "unexpected_type: Allows to copy-construct from error_ unexpected_type u{ ep }; - EXPECT( u.value() == ep ); + EXPECT( u.error() == ep ); } CASE( "unexpected_type: Allows to move-construct from error_type" ) @@ -422,7 +422,7 @@ CASE( "unexpected_type: Allows to move-construct from error_ unexpected_type u{ std::move( ep_move ) }; - EXPECT( u.value() == ep_copy ); + EXPECT( u.error() == ep_copy ); } CASE( "unexpected_type: Allows to copy-construct from an exception" ) @@ -433,7 +433,7 @@ CASE( "unexpected_type: Allows to copy-construct from an exc try { - std::rethrow_exception( u.value() ); + std::rethrow_exception( u.error() ); } catch( std::exception const & e ) { @@ -446,7 +446,7 @@ CASE( "unexpected_type: Allows to observe its value" ) const auto ep = make_ep(); unexpected_type u{ ep }; - EXPECT( u.value() == ep ); + EXPECT( u.error() == ep ); } CASE( "unexpected_type: Allows to modify its value" ) @@ -455,9 +455,9 @@ CASE( "unexpected_type: Allows to modify its value" ) const auto ep2 = make_ep(); unexpected_type u{ ep1 }; - u.value() = ep2; + u.error() = ep2; - EXPECT( u.value() == ep2 ); + EXPECT( u.error() == ep2 ); } //CASE( "unexpected_type: Allows reset via = {}, std::exception_ptr specialization" ) @@ -539,7 +539,7 @@ CASE( "make_unexpected(): Allows to create an unexpected_type from an E" ) const auto error = 7; auto u = make_unexpected( error ); - EXPECT( u.value() == error ); + EXPECT( u.error() == error ); } CASE( "make_unexpected_from_current_exception(): Allows to create an unexpected_type from the current exception" "[.deprecated]" ) @@ -557,7 +557,7 @@ CASE( "make_unexpected_from_current_exception(): Allows to create an unexpected_ try { - std::rethrow_exception( u.value() ); + std::rethrow_exception( u.error() ); } catch( std::exception const & e ) { @@ -1191,7 +1191,7 @@ CASE( "expected: Allows to observe its error as unexpected" ) const auto v = 7; expected e{ unexpect, v }; - EXPECT( e.get_unexpected().value() == v ); + EXPECT( e.get_unexpected().error() == v ); } CASE( "expected: Allows to query if it contains an exception of a specific base type" ) @@ -1632,7 +1632,7 @@ CASE( "expected: Allows to observe its error as unexpected" ) const auto value = 7; expected e{ unexpect, value }; - EXPECT( e.get_unexpected().value() == value ); + EXPECT( e.get_unexpected().error() == value ); } CASE( "expected: Allows to query if it contains an exception of a specific base type" ) @@ -2171,7 +2171,7 @@ CASE( "issue-58" ) EXPECT( !unexpected.has_value() ); } -#if nsel_P2505R >= 3 +#if !nsel_USES_STD_EXPECTED && nsel_P2505R >= 3 CASE( "invoke" ) { struct A { @@ -2198,7 +2198,7 @@ CASE( "invoke" ) EXPECT( nonstd::expected_lite::detail::invoke(&A::get2, ref, 'a') == 12 ); EXPECT( nonstd::expected_lite::detail::invoke(&A::get2, cref, 'a') == 7 ); } -#endif // nsel_P2505R >= 3 +#endif // nsel_USES_STD_EXPECTED && nsel_P2505R >= 3 // ----------------------------------------------------------------------- // using as optional