Compare commits

...

14 Commits

Author SHA1 Message Date
Peter Dimov
f80b9ccbf1 Update documentation 2026-03-03 20:14:14 +02:00
Peter Dimov
2db6b8e595 Add unwrap_and_invoke.hpp to boost/system.hpp 2026-03-03 19:36:13 +02:00
Peter Dimov
026862285e Disable -Wmissing-field-initializers in test/unwrap_and_construct2 2026-02-25 20:02:57 +02:00
Peter Dimov
f7408130d0 Members with initializers make a class non-aggregate in C++11 2026-02-25 19:31:23 +02:00
Peter Dimov
59b11b120d Disable test/unwrap_and_construct2 when detail::is_aggregate isn't functional 2026-02-25 18:52:28 +02:00
Peter Dimov
8ce9b50730 Support aggregates in unwrap_and_construct 2026-02-25 18:19:49 +02:00
Peter Dimov
700f246dd3 Rename BOOST_SYSTEM_HAS_IS_AGGREGATE to BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE 2026-02-25 17:54:22 +02:00
Peter Dimov
78149b00c6 Disable detail_is_aggregate_test when detail::is_aggregate isn't functional 2026-02-25 17:52:23 +02:00
Peter Dimov
9c1c586a32 Add detail::is_aggregate 2026-02-25 16:52:10 +02:00
Peter Dimov
d9248a93ab Placate GCC 4.x 2026-02-25 16:40:15 +02:00
Peter Dimov
fa3babf4e6 Add unwrap_and_construct 2026-02-25 14:10:14 +02:00
Peter Dimov
739179a494 Add test/unwrap_and_invoke2.cpp 2026-02-25 02:30:48 +02:00
Peter Dimov
ee43711c1c std::make_unique is C++14 2026-02-24 20:03:04 +02:00
Peter Dimov
af53bce037 Add unwrap_and_invoke.hpp 2026-02-24 18:59:48 +02:00
12 changed files with 962 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ https://www.boost.org/LICENSE_1_0.txt
* Added `unsafe_value` to `result`.
* Changed `result<>::operator*` and `result<>::operator\->` to throw when `!has_value()`,
instead of having that as a precondition. The old behavior is now spelled `unsafe_value()`.
* Added `boost/system/unwrap_and_invoke.hpp`.
## Changes in Boost 1.89

View File

@@ -2911,6 +2911,82 @@ Returns: :: `r`.
Remarks: ::
Only enabled when `R` is an instance of `result<void, E2>` and `E2` is convertible to `E`.
## <boost/system/{zwsp}unwrap_and_invoke.hpp>
```
namespace boost {
namespace system {
template<class F, class... A>
auto unwrap_and_invoke( F&& f, A&&... a ) -> result</*...*/>;
template<class T, class... A>
auto unwrap_and_construct( A&&... a ) -> /*...*/;
} // namespace system
} // namespace boost
```
### unwrap_and_invoke
```
template<class F, class... A>
auto unwrap_and_invoke( F&& f, A&&... a ) -> result</*...*/>;
```
[none]
* {blank}
+
Mandates: ::
At least one of the arguments in `a...` must be an instance of `result`.
+
All arguments in `a...` that are instances of `result` must have the same error type.
+
Effects: ::
If `ai`, one of the arguments in `a...`, is an instance of `result` for which `has_error()` is `true`, returns `ai.error()`.
+
Otherwise, returns `f(b...)`, where `bi` is `*ai` if `ai` is an instance of `result`, `ai` otherwise.
+
Remarks: ::
The return type is `result<R, E>`, where `R` is the type of the application of `f`, and `E` is the common error type of the arguments in `a...` that are instances of `result`.
### unwrap_and_construct
```
template<class T, class... A>
auto unwrap_and_construct( A&&... a ) -> /*...*/;
```
[none]
* {blank}
+
Like `unwrap_and_invoke`, but instead of a function object, invokes a constructor of the passed type `T`.
+
Returns: :: `unwrap_and_invoke(cf, a...)`, where `cf` is a function object such that `cf(args...)` returns `T{args...}` when `T` is an aggregate, `T(args...)` otherwise.
+
Example: ::
+
```
struct JsonValue
{
result<JsonValue const&> at( std::string_view key ) const noexcept;
};
result<int> int_from_json( JsonValue const& jv ) noexcept;
struct X
{
int a;
int b;
};
result<X> X_from_json( JsonValue const& jv ) noexcept
{
return unwrap_and_construct<X>(
jv.at( "a" ) & int_from_json,
jv.at( "b" ) & int_from_json );
}
```
## <boost/system.hpp>
This convenience header includes all the headers previously described.

View File

@@ -10,5 +10,6 @@
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/result.hpp>
#include <boost/system/unwrap_and_invoke.hpp>
#endif // #ifndef BOOST_SYSTEM_HPP_INCLUDED

View File

@@ -0,0 +1,60 @@
#ifndef BOOST_SYSTEM_DETAIL_IS_AGGREGATE_HPP_INCLUDED
#define BOOST_SYSTEM_DETAIL_IS_AGGREGATE_HPP_INCLUDED
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#include <type_traits>
#if defined(__has_builtin)
# if __has_builtin(__is_aggregate)
# define BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE
# endif
#endif
#if !defined(BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE) && defined(BOOST_CLANG_VERSION) && BOOST_CLANG_VERSION >= 50000
# define BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE
#endif
#if !defined(BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE) && defined(BOOST_GCC) && BOOST_GCC >= 70000
# define BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE
#endif
#if !defined(BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE) && defined(BOOST_MSVC) && BOOST_MSVC >= 1910
# define BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE
#endif
namespace boost
{
namespace system
{
namespace detail
{
#if defined(BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE)
template<class T> struct is_aggregate: public std::integral_constant<bool, __is_aggregate(T)>
{
};
#elif defined(__cpp_lib_is_aggregate) && __cpp_lib_is_aggregate >= 201703L
template<class T> struct is_aggregate: public std::is_aggregate<T>
{
};
#else
template<class T> struct is_aggregate: public std::false_type
{
};
#endif
} // namespace detail
} // namespace system
} // namespace boost
#endif // #ifndef BOOST_SYSTEM_DETAIL_IS_AGGREGATE_HPP_INCLUDED

View File

@@ -0,0 +1,124 @@
#ifndef BOOST_SYSTEM_UNWRAP_AND_INVOKE_HPP_INCLUDED
#define BOOST_SYSTEM_UNWRAP_AND_INVOKE_HPP_INCLUDED
// Copyright 2026 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/system/detail/is_aggregate.hpp>
#include <boost/compat/type_traits.hpp>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/utility.hpp>
#include <type_traits>
#include <utility>
namespace boost
{
namespace system
{
// unwrap_and_invoke
namespace detail
{
// get_error_type
template<class... T> using first_if_same =
mp11::mp_if<mp11::mp_same<T...>, mp11::mp_first<mp11::mp_list<T...>>>;
template<class... A> using get_error_type =
mp11::mp_apply<first_if_same,
mp11::mp_transform<mp11::mp_second,
mp11::mp_copy_if<mp11::mp_list<A...>, is_result>
>
>;
// invoke_unwrap
template< class T, class En = typename std::enable_if< !is_result< compat::remove_cvref_t<T> >::value >::type >
auto invoke_unwrap( T&& t ) noexcept -> T&&
{
return std::forward<T>( t );
}
template< class T, class = void, class En = typename std::enable_if< is_result< compat::remove_cvref_t<T> >::value >::type >
auto invoke_unwrap( T&& t ) noexcept -> decltype( std::forward<T>( t ).unsafe_value() )
{
return std::forward<T>( t ).unsafe_value();
}
// invoke_test
template<class R, class A> int invoke_test( R&, A const& )
{
return 0;
}
template<class R, class T, class E> int invoke_test( R& r, result<T, E> const& r2 )
{
if( r && r2.has_error() ) r = r2.error();
return 0;
}
} // namespace detail
template<class F, class... A,
class R = decltype( compat::invoke( std::declval<F>(), detail::invoke_unwrap( std::declval<A>() )... ) ),
class E = detail::get_error_type<compat::remove_cvref_t<A>...>
>
auto unwrap_and_invoke( F&& f, A&&... a ) -> result<R, E>
{
{
result<void, E> r;
using Q = int[];
(void)Q{ detail::invoke_test( r, a )... };
if( !r ) return r.error();
}
return compat::invoke( std::forward<F>(f), detail::invoke_unwrap( std::forward<A>(a) )... );
}
// unwrap_and_construct
namespace detail
{
template<class T> struct construct
{
private:
template<class... A> static inline T call_impl( std::false_type, A&&... a )
{
return T( std::forward<A>(a)... );
}
template<class... A> static inline T call_impl( std::true_type, A&&... a )
{
return T{ std::forward<A>(a)... };
}
public:
template<class... A> inline T operator()( A&&... a ) const
{
return this->call_impl( detail::is_aggregate<T>(), std::forward<A>(a)... );
}
};
} // namespace detail
template<class T, class... A>
auto unwrap_and_construct( A&&... a )
-> decltype( unwrap_and_invoke( detail::construct<T>(), std::forward<A>(a)... ) )
{
return unwrap_and_invoke( detail::construct<T>(), std::forward<A>(a)... );
}
} // namespace system
} // namespace boost
#endif // #ifndef BOOST_SYSTEM_UNWRAP_AND_INVOKE_HPP_INCLUDED

View File

@@ -187,3 +187,11 @@ boost_test(TYPE compile SOURCES result_error_construct_cx2.cpp)
boost_test(TYPE run SOURCES result_unsafe_value_access.cpp)
boost_test(TYPE run SOURCES result_unsafe_value_access2.cpp)
boost_test(TYPE run SOURCES unwrap_and_invoke.cpp)
boost_test(TYPE run SOURCES unwrap_and_invoke2.cpp)
boost_test(TYPE run SOURCES unwrap_and_construct.cpp)
boost_test(TYPE run SOURCES unwrap_and_construct2.cpp)
boost_test(TYPE run SOURCES detail_is_aggregate_test.cpp)

View File

@@ -220,3 +220,11 @@ compile result_error_construct_cx2.cpp ;
run result_unsafe_value_access.cpp ;
run result_unsafe_value_access2.cpp ;
run unwrap_and_invoke.cpp ;
run unwrap_and_invoke2.cpp ;
run unwrap_and_construct.cpp ;
run unwrap_and_construct2.cpp ;
run detail_is_aggregate_test.cpp ;

View File

@@ -0,0 +1,57 @@
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/system/detail/is_aggregate.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/config/pragma_message.hpp>
#include <string>
struct X1
{
};
struct X2
{
int a, b;
};
struct X3
{
std::string a, b;
};
struct X4
{
X1 x1;
X2 x2;
X3 x3;
};
struct Y1
{
Y1() {}
};
#if !defined(BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE) && !( defined(__cpp_lib_is_aggregate) && __cpp_lib_is_aggregate >= 201703L )
BOOST_PRAGMA_MESSAGE("Test skipped, detail::is_aggregate isn't functional")
int main() {}
#else
int main()
{
using boost::system::detail::is_aggregate;
BOOST_TEST_TRAIT_TRUE((is_aggregate<X1>));
BOOST_TEST_TRAIT_TRUE((is_aggregate<X2>));
BOOST_TEST_TRAIT_TRUE((is_aggregate<X3>));
BOOST_TEST_TRAIT_TRUE((is_aggregate<X4>));
BOOST_TEST_TRAIT_FALSE((is_aggregate<Y1>));
return boost::report_errors();
}
#endif

View File

@@ -0,0 +1,109 @@
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/system/unwrap_and_invoke.hpp>
#include <boost/system/result.hpp>
#include <boost/core/lightweight_test_trait.hpp>
using namespace boost::system;
struct X
{
int v;
};
struct Y
{
int v;
explicit Y( X a1 = {0}, X a2 = {0}, X a3 = {0}, X a4 = {0} ): v( a1.v + a2.v + a3.v + a4.v )
{
}
};
struct E
{
int w;
};
int main()
{
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 1 );
}
//
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ), result<X, E>( E{2} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( E{2} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 3 );
}
//
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 3 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 6 );
}
//
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 3 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 4 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ), result<X, E>( X{4} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 10 );
}
return boost::report_errors();
}

View File

@@ -0,0 +1,144 @@
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
#include <boost/system/unwrap_and_invoke.hpp>
#include <boost/system/result.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/config/pragma_message.hpp>
#include <boost/config.hpp>
struct X
{
int v;
};
struct Y
{
X x1, x2, x3, x4;
};
struct E
{
int w;
};
#if !defined(BOOST_SYSTEM_HAS_BUILTIN_IS_AGGREGATE) && !( defined(__cpp_lib_is_aggregate) && __cpp_lib_is_aggregate >= 201703L )
BOOST_PRAGMA_MESSAGE("Test skipped, detail::is_aggregate isn't functional")
int main() {}
#else
using namespace boost::system;
int main()
{
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ) );
BOOST_TEST( r )
&& BOOST_TEST_EQ( r.unsafe_value().x1.v, 1 )
&& BOOST_TEST_EQ( r.unsafe_value().x2.v, 0 )
&& BOOST_TEST_EQ( r.unsafe_value().x3.v, 0 )
&& BOOST_TEST_EQ( r.unsafe_value().x4.v, 0 )
;
}
//
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ), result<X, E>( E{2} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( E{2} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ) );
BOOST_TEST( r )
&& BOOST_TEST_EQ( r.unsafe_value().x1.v, 1 )
&& BOOST_TEST_EQ( r.unsafe_value().x2.v, 2 )
&& BOOST_TEST_EQ( r.unsafe_value().x3.v, 0 )
&& BOOST_TEST_EQ( r.unsafe_value().x4.v, 0 )
;
}
//
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 3 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ) );
BOOST_TEST( r )
&& BOOST_TEST_EQ( r.unsafe_value().x1.v, 1 )
&& BOOST_TEST_EQ( r.unsafe_value().x2.v, 2 )
&& BOOST_TEST_EQ( r.unsafe_value().x3.v, 3 )
&& BOOST_TEST_EQ( r.unsafe_value().x4.v, 0 )
;
}
//
{
auto r = unwrap_and_construct<Y>( result<X, E>( E{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 3 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 4 );
}
{
auto r = unwrap_and_construct<Y>( result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ), result<X, E>( X{4} ) );
BOOST_TEST( r )
&& BOOST_TEST_EQ( r.unsafe_value().x1.v, 1 )
&& BOOST_TEST_EQ( r.unsafe_value().x2.v, 2 )
&& BOOST_TEST_EQ( r.unsafe_value().x3.v, 3 )
&& BOOST_TEST_EQ( r.unsafe_value().x4.v, 4 )
;
}
return boost::report_errors();
}
#endif

254
test/unwrap_and_invoke.cpp Normal file
View File

@@ -0,0 +1,254 @@
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/system/unwrap_and_invoke.hpp>
#include <boost/system/result.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <memory>
using namespace boost::system;
int f( int x, int y )
{
return x + y;
}
struct X
{
int x_ = 0;
explicit X( int x ): x_( x )
{
}
int f( int y ) const
{
return x_ + y;
}
int g( std::unique_ptr<X> p ) const
{
return x_ + p->x_;
}
};
template<class T, class... A> std::unique_ptr<T> make_unique( A&&... a )
{
return std::unique_ptr<T>( new T( std::forward<A>(a)... ) );
}
int main()
{
{
result<int> a1( 1 );
auto r = unwrap_and_invoke( f, a1, 2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
result<int> const a1( 1 );
auto r = unwrap_and_invoke( f, a1, 2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
result<int const> a1( 1 );
auto r = unwrap_and_invoke( f, a1, 2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
auto r = unwrap_and_invoke( f, result<int>( 1 ), 2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
//
{
result<int> a2( 2 );
auto r = unwrap_and_invoke( f, 1, a2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
result<int> const a2( 2 );
auto r = unwrap_and_invoke( f, 1, a2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
result<int const> a2( 2 );
auto r = unwrap_and_invoke( f, 1, a2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
auto r = unwrap_and_invoke( f, 1, result<int>( 2 ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
//
{
result<int> a1( 1 );
result<int> const a2( 2 );
auto r = unwrap_and_invoke( f, a1, a2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
result<int const> a1( 1 );
auto r = unwrap_and_invoke( f, a1, result<int>( 2 ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
//
{
auto ec = make_error_code( errc::invalid_argument );
result<int> a1( ec );
auto r = unwrap_and_invoke( f, a1, 2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int> const a1( ec );
auto r = unwrap_and_invoke( f, a1, 2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int const> a1( ec );
auto r = unwrap_and_invoke( f, a1, 2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
auto r = unwrap_and_invoke( f, result<int>( ec ), 2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
//
{
auto ec = make_error_code( errc::invalid_argument );
result<int> a2( ec );
auto r = unwrap_and_invoke( f, 1, a2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int> const a2( ec );
auto r = unwrap_and_invoke( f, 1, a2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int const> a2( ec );
auto r = unwrap_and_invoke( f, 1, a2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
auto r = unwrap_and_invoke( f, 1, result<int>( ec ) );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
//
{
auto ec = make_error_code( errc::invalid_argument );
result<int> a1( ec );
result<int> const a2( 2 );
auto r = unwrap_and_invoke( f, a1, a2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int> a1( 1 );
result<int> const a2( ec );
auto r = unwrap_and_invoke( f, a1, a2 );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int const> a1( ec );
auto r = unwrap_and_invoke( f, a1, result<int>( 2 ) );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
{
auto ec = make_error_code( errc::invalid_argument );
result<int const> a1( 1 );
auto r = unwrap_and_invoke( f, a1, result<int>( ec ) );
BOOST_TEST( r.has_error() ) && BOOST_TEST_EQ( r.error(), ec );
}
//
{
result<X> a1( X( 1 ) );
result<int> const a2( 2 );
auto r = unwrap_and_invoke( &X::f, a1, a2 );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
{
result<X const> a1( X( 1 ) );
auto r = unwrap_and_invoke( &X::f, a1, result<int>( 2 ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
//
{
result< std::unique_ptr<X> > a1( ::make_unique<X>( 1 ) );
result< std::unique_ptr<X> > a2( ::make_unique<X>( 2 ) );
auto r = unwrap_and_invoke( &X::g, std::move( a1 ), std::move( a2 ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( *r, 3 );
}
return boost::report_errors();
}

120
test/unwrap_and_invoke2.cpp Normal file
View File

@@ -0,0 +1,120 @@
// Copyright 2026 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/system/unwrap_and_invoke.hpp>
#include <boost/system/result.hpp>
#include <boost/core/lightweight_test_trait.hpp>
using namespace boost::system;
struct X
{
int v;
};
struct E
{
int w;
};
X f1( X x1 )
{
return x1;
}
X f2( X x1, X x2 )
{
return X{ x1.v + x2.v };
}
X f3( X x1, X x2, X x3 )
{
return X{ x1.v + x2.v + x3.v };
}
X f4( X x1, X x2, X x3, X x4 )
{
return X{ x1.v + x2.v + x3.v + x4.v };
}
int main()
{
{
auto r = unwrap_and_invoke( f1, result<X, E>( E{1} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_invoke( f1, result<X, E>( X{1} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 1 );
}
//
{
auto r = unwrap_and_invoke( f2, result<X, E>( E{1} ), result<X, E>( E{2} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_invoke( f2, result<X, E>( X{1} ), result<X, E>( E{2} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_invoke( f2, result<X, E>( X{1} ), result<X, E>( X{2} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 3 );
}
//
{
auto r = unwrap_and_invoke( f3, result<X, E>( E{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_invoke( f3, result<X, E>( X{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_invoke( f3, result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( E{3} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 3 );
}
{
auto r = unwrap_and_invoke( f3, result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 6 );
}
//
{
auto r = unwrap_and_invoke( f4, result<X, E>( E{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 1 );
}
{
auto r = unwrap_and_invoke( f4, result<X, E>( X{1} ), result<X, E>( E{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 2 );
}
{
auto r = unwrap_and_invoke( f4, result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( E{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 3 );
}
{
auto r = unwrap_and_invoke( f4, result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ), result<X, E>( E{4} ) );
BOOST_TEST( !r ) && BOOST_TEST_EQ( r.error().w, 4 );
}
{
auto r = unwrap_and_invoke( f4, result<X, E>( X{1} ), result<X, E>( X{2} ), result<X, E>( X{3} ), result<X, E>( X{4} ) );
BOOST_TEST( r ) && BOOST_TEST_EQ( r.unsafe_value().v, 10 );
}
return boost::report_errors();
}