Add operator|( result, nullary-returning-value )

This commit is contained in:
Peter Dimov
2023-10-29 02:32:19 +03:00
parent 0cd351014b
commit c1fa3619b6
4 changed files with 198 additions and 0 deletions

View File

@ -935,6 +935,40 @@ template<class T, class E, class U,
}
}
// result | nullary-returning-value
template<class T, class E, class F,
class U = decltype( std::declval<F>()() ),
class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
>
T operator|( result<T, E> const& r, F&& f )
{
if( r )
{
return *r;
}
else
{
return std::forward<F>( f )();
}
}
template<class T, class E, class F,
class U = decltype( std::declval<F>()() ),
class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
>
T operator|( result<T, E>&& r, F&& f )
{
if( r )
{
return *std::move( r );
}
else
{
return std::forward<F>( f )();
}
}
} // namespace system
} // namespace boost

View File

@ -170,3 +170,4 @@ boost_test(TYPE run SOURCES result_error_construct5.cpp)
boost_test(TYPE run SOURCES result_or_value.cpp)
boost_test(TYPE compile-fail SOURCES result_or_value_fail.cpp)
boost_test(TYPE compile-fail SOURCES result_or_value_fail2.cpp)
boost_test(TYPE run SOURCES result_or_fn0v.cpp)

View File

@ -200,3 +200,4 @@ run result_error_construct5.cpp : : : $(CPP11) ;
run result_or_value.cpp : : : $(CPP11) ;
compile-fail result_or_value_fail.cpp : $(CPP11) ;
compile-fail result_or_value_fail2.cpp : $(CPP11) ;
run result_or_fn0v.cpp : : : $(CPP11) ;

162
test/result_or_fn0v.cpp Normal file
View File

@ -0,0 +1,162 @@
// Copyright 2017, 2021, 2022 Peter Dimov.
// 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>
using namespace boost::system;
struct X
{
int v_;
};
struct Y
{
int v_;
explicit Y( int v ): v_( v ) {}
Y( X x ): v_( x.v_) {}
Y( Y const& ) = delete;
Y& operator=( Y const& ) = delete;
Y( Y&& r ): v_( r.v_ )
{
r.v_ = 0;
}
Y& operator=( Y&& ) = delete;
};
struct E
{
};
int f()
{
return 2;
}
X g()
{
return { 2 };
}
int& h()
{
static int x = 2;
return x;
}
int main()
{
{
result<int> r( 1 );
int x = r | f;
BOOST_TEST_EQ( x, 1 );
}
{
result<int> const r( 1 );
int x = r | f;
BOOST_TEST_EQ( x, 1 );
}
{
int x = result<int>( 1 ) | f;
BOOST_TEST_EQ( x, 1 );
}
{
result<int, E> r( in_place_error );
int x = r | f;
BOOST_TEST_EQ( x, 2 );
}
{
result<int, E> const r( in_place_error );
int x = r | f;
BOOST_TEST_EQ( x, 2 );
}
{
int x = result<int, E>( in_place_error ) | f;
BOOST_TEST_EQ( x, 2 );
}
{
Y y = result<Y>( in_place_value, 1 ) | g;
BOOST_TEST_EQ( y.v_, 1 );
}
{
Y y = result<Y, E>( in_place_error ) | g;
BOOST_TEST_EQ( y.v_, 2 );
}
{
int x1 = 1;
result<int&> r( x1 );
int& x = r | h;
BOOST_TEST_EQ( &x, &x1 );
}
{
int x1 = 1;
result<int&> const r( x1 );
int& x = r | h;
BOOST_TEST_EQ( &x, &x1 );
}
{
int x1 = 1;
int& x = result<int&>( x1 ) | h;
BOOST_TEST_EQ( &x, &x1 );
}
{
result<int&, E> r( in_place_error );
int& x = r | h;
BOOST_TEST_EQ( &x, &h() );
}
{
result<int&, E> const r( in_place_error );
int& x = r | h;
BOOST_TEST_EQ( &x, &h() );
}
{
int& x = result<int&, E>( in_place_error ) | h;
BOOST_TEST_EQ( &x, &h() );
}
return boost::report_errors();
}