mirror of
https://github.com/boostorg/system.git
synced 2025-07-29 20:17:13 +02:00
Add operator|=(result&, fn0v)
This commit is contained in:
@ -1084,6 +1084,22 @@ result<T, E>& operator|=( result<T, E>& r, U&& u )
|
||||
return r;
|
||||
}
|
||||
|
||||
// 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
|
||||
>
|
||||
result<T, E>& operator|=( result<T, E>& r, F&& f )
|
||||
{
|
||||
if( !r )
|
||||
{
|
||||
r = std::forward<F>( f )();
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
// operator&
|
||||
|
||||
// result & unary-returning-value
|
||||
|
@ -175,3 +175,4 @@ boost_test(TYPE run SOURCES result_and_eq_fn1v.cpp)
|
||||
boost_test(TYPE run SOURCES result_and_eq_fn1r.cpp)
|
||||
boost_test(TYPE run SOURCES result_in_place_use.cpp)
|
||||
boost_test(TYPE run SOURCES result_or_eq_value.cpp)
|
||||
boost_test(TYPE run SOURCES result_or_eq_fn0v.cpp)
|
||||
|
@ -205,3 +205,4 @@ run result_and_eq_fn1v.cpp ;
|
||||
run result_and_eq_fn1r.cpp ;
|
||||
run result_in_place_use.cpp ;
|
||||
run result_or_eq_value.cpp ;
|
||||
run result_or_eq_fn0v.cpp ;
|
||||
|
115
test/result_or_eq_fn0v.cpp
Normal file
115
test/result_or_eq_fn0v.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
// Copyright 2017, 2021-2024 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&& r )
|
||||
{
|
||||
if( &r != this )
|
||||
{
|
||||
v_ = r.v_;
|
||||
r.v_ = 0;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
struct E
|
||||
{
|
||||
};
|
||||
|
||||
int f()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
X g()
|
||||
{
|
||||
return { 2 };
|
||||
}
|
||||
|
||||
int& h()
|
||||
{
|
||||
static int x = 2;
|
||||
return x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
result<int> r( 1 );
|
||||
|
||||
r |= f;
|
||||
|
||||
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, 1 );
|
||||
}
|
||||
|
||||
{
|
||||
result<int, E> r( in_place_error );
|
||||
|
||||
r |= f;
|
||||
|
||||
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, 2 );
|
||||
}
|
||||
|
||||
{
|
||||
result<Y> r( in_place_value, 1 );
|
||||
|
||||
r |= g;
|
||||
|
||||
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( r->v_, 1 );
|
||||
}
|
||||
|
||||
{
|
||||
result<Y, E> r( in_place_error );
|
||||
|
||||
r |= g;
|
||||
|
||||
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( r->v_, 2 );
|
||||
}
|
||||
|
||||
{
|
||||
int x1 = 1;
|
||||
|
||||
result<int&> r( x1 );
|
||||
|
||||
r |= h;
|
||||
|
||||
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( &*r, &x1 );
|
||||
}
|
||||
|
||||
{
|
||||
result<int&, E> r( in_place_error );
|
||||
|
||||
r |= h;
|
||||
|
||||
BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( &*r, &h() );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user