Made result::error movable.

Closes #108.
This commit is contained in:
Klemens
2023-03-08 08:07:38 +08:00
parent ee80491cab
commit c5c49894e6
5 changed files with 126 additions and 3 deletions

View File

@ -1657,7 +1657,8 @@ public:
// error access
constexpr E error() const;
constexpr E error() const &;
constexpr E error() &&;
// emplace

View File

@ -401,12 +401,18 @@ public:
// error access
constexpr E error() const
constexpr E error() const &
noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
{
return has_error()? variant2::unsafe_get<1>( v_ ): E();
}
BOOST_CXX14_CONSTEXPR E error() &&
noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_move_constructible<E>::value )
{
return has_error()? std::move( variant2::unsafe_get<1>( v_ ) ): E();
}
// emplace
template<class... A>
@ -581,12 +587,18 @@ public:
// error access
constexpr E error() const
constexpr E error() const &
noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_copy_constructible<E>::value )
{
return has_error()? variant2::unsafe_get<1>( v_ ): E();
}
BOOST_CXX14_CONSTEXPR E error() &&
noexcept( std::is_nothrow_default_constructible<E>::value && std::is_nothrow_move_constructible<E>::value )
{
return has_error()? std::move( variant2::unsafe_get<1>( v_ ) ): E();
}
// emplace
BOOST_CXX14_CONSTEXPR void emplace()

View File

@ -163,3 +163,4 @@ boost_test(TYPE run SOURCES result_emplace.cpp)
boost_test(TYPE run SOURCES result_error_construct4.cpp)
boost_test(TYPE run SOURCES result_value_construct4.cpp)
boost_test(TYPE run SOURCES result_value_construct5.cpp)
boost_test(TYPE run SOURCES result_error_move.cpp)

View File

@ -193,3 +193,4 @@ run result_emplace.cpp : : : $(CPP11) ;
run result_error_construct4.cpp : : : $(CPP11) ;
run result_value_construct4.cpp : : : $(CPP11) ;
run result_value_construct5.cpp : : : $(CPP11) ;
run result_error_move.cpp : : : $(CPP11) ;

108
test/result_error_move.cpp Normal file
View File

@ -0,0 +1,108 @@
// Copyright 2023 Klemens Morgenstern
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/system/result.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <string>
using namespace boost::system;
struct XM
{
int v_;
explicit XM( int v = 0 ): v_( v ) {}
XM( XM const& ) = delete;
XM& operator=( XM const& ) = delete;
XM( XM && ) = default;
XM& operator=( XM && ) = default;
};
int main()
{
{
result<std::string, XM> r( 1 );
BOOST_TEST( !r.has_value() );
BOOST_TEST( r.has_error() );
BOOST_TEST_EQ( std::move( r ).error().v_, 1 );
}
{
BOOST_TEST(( !result<std::string, XM>( 1 ).has_value() ));
BOOST_TEST(( result<std::string, XM>( 1 ).has_error() ));
BOOST_TEST_EQ( (result<std::string, XM>( 1 ).error().v_), 1 );
}
{
result<std::string, XM> r( "s" );
BOOST_TEST( r.has_value() );
BOOST_TEST( !r.has_error() );
BOOST_TEST_EQ( std::move( r ).error().v_, 0 );
}
{
result<std::string, XM> r( "s" );
BOOST_TEST( r.has_value() );
BOOST_TEST( !r.has_error() );
BOOST_TEST_EQ( std::move( r ).error().v_, 0 );
}
{
BOOST_TEST(( result<std::string, XM>( "s" ).has_value() ));
BOOST_TEST(( !result<std::string, XM>( "s" ).has_error() ));
BOOST_TEST_EQ( (result<std::string, XM>( "s" ).error().v_), 0 );
}
{
result<void, XM> r( 1 );
BOOST_TEST( !r.has_value() );
BOOST_TEST( r.has_error() );
BOOST_TEST_EQ( std::move( r ).error().v_, 1 );
}
{
BOOST_TEST(( !result<void, XM>( 1 ).has_value() ));
BOOST_TEST(( result<void, XM>( 1 ).has_error() ));
BOOST_TEST_EQ( (result<void, XM>( 1 ).error().v_), 1 );
}
{
result<void, XM> r;
BOOST_TEST( r.has_value() );
BOOST_TEST( !r.has_error() );
BOOST_TEST_EQ( std::move( r ).error().v_, 0 );
}
{
result<void, XM> r;
BOOST_TEST( r.has_value() );
BOOST_TEST( !r.has_error() );
BOOST_TEST_EQ( std::move( r ).error().v_, 0 );
}
{
BOOST_TEST(( result<void, XM>( ).has_value() ));
BOOST_TEST(( !result<void, XM>( ).has_error() ));
BOOST_TEST_EQ( (result<void, XM>( ).error().v_), 0 );
}
return boost::report_errors();
}