mirror of
https://github.com/boostorg/system.git
synced 2025-07-30 12:37:13 +02:00
Enable implicit construction for aggregates using {{ ... }}
This commit is contained in:
@ -62,35 +62,10 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
// explicit, value
|
||||
template<class A, class En = typename std::enable_if<
|
||||
std::is_constructible<T, A>::value &&
|
||||
!std::is_convertible<A, T>::value &&
|
||||
!std::is_constructible<E, A>::value
|
||||
>::type>
|
||||
explicit constexpr result( A&& a )
|
||||
noexcept( std::is_nothrow_constructible<T, A>::value )
|
||||
: v_( in_place_value, std::forward<A>(a) )
|
||||
{
|
||||
}
|
||||
|
||||
// explicit, error
|
||||
template<class A, class En2 = void, class En = typename std::enable_if<
|
||||
std::is_constructible<E, A>::value &&
|
||||
!std::is_convertible<A, E>::value &&
|
||||
!std::is_constructible<T, A>::value
|
||||
>::type>
|
||||
explicit constexpr result( A&& a )
|
||||
noexcept( std::is_nothrow_constructible<E, A>::value )
|
||||
: v_( in_place_error, std::forward<A>(a) )
|
||||
{
|
||||
}
|
||||
|
||||
// implicit, value
|
||||
template<class A, class En2 = void, class En3 = void, class En = typename std::enable_if<
|
||||
template<class A = T, typename std::enable_if<
|
||||
std::is_convertible<A, T>::value &&
|
||||
!std::is_constructible<E, A>::value
|
||||
>::type>
|
||||
!std::is_constructible<E, A>::value, int>::type = 0>
|
||||
constexpr result( A&& a )
|
||||
noexcept( std::is_nothrow_constructible<T, A>::value )
|
||||
: v_( in_place_value, std::forward<A>(a) )
|
||||
@ -98,35 +73,32 @@ public:
|
||||
}
|
||||
|
||||
// implicit, error
|
||||
template<class A, class En2 = void, class En3 = void, class En4 = void, class En = typename std::enable_if<
|
||||
template<class A = E, class = void, typename std::enable_if<
|
||||
std::is_convertible<A, E>::value &&
|
||||
!std::is_constructible<T, A>::value
|
||||
>::type>
|
||||
!std::is_constructible<T, A>::value, int>::type = 0>
|
||||
constexpr result( A&& a )
|
||||
noexcept( std::is_nothrow_constructible<E, A>::value )
|
||||
: v_( in_place_error, std::forward<A>(a) )
|
||||
{
|
||||
}
|
||||
|
||||
// more than one arg, value
|
||||
// explicit, value
|
||||
template<class... A, class En = typename std::enable_if<
|
||||
std::is_constructible<T, A...>::value &&
|
||||
!std::is_constructible<E, A...>::value &&
|
||||
sizeof...(A) >= 2
|
||||
!std::is_constructible<E, A...>::value
|
||||
>::type>
|
||||
constexpr result( A&&... a )
|
||||
explicit constexpr result( A&&... a )
|
||||
noexcept( std::is_nothrow_constructible<T, A...>::value )
|
||||
: v_( in_place_value, std::forward<A>(a)... )
|
||||
{
|
||||
}
|
||||
|
||||
// more than one arg, error
|
||||
// explicit, error
|
||||
template<class... A, class En2 = void, class En = typename std::enable_if<
|
||||
!std::is_constructible<T, A...>::value &&
|
||||
std::is_constructible<E, A...>::value &&
|
||||
sizeof...(A) >= 2
|
||||
std::is_constructible<E, A...>::value
|
||||
>::type>
|
||||
constexpr result( A&&... a )
|
||||
explicit constexpr result( A&&... a )
|
||||
noexcept( std::is_nothrow_constructible<E, A...>::value )
|
||||
: v_( in_place_error, std::forward<A>(a)... )
|
||||
{
|
||||
|
@ -126,3 +126,5 @@ boost_test(TYPE run SOURCES result_error_access.cpp)
|
||||
boost_test(TYPE run SOURCES result_swap.cpp)
|
||||
boost_test(TYPE run SOURCES result_eq.cpp)
|
||||
boost_test(TYPE run SOURCES result_range_for.cpp)
|
||||
boost_test(TYPE run SOURCES result_value_construct2.cpp)
|
||||
boost_test(TYPE run SOURCES result_error_construct2.cpp)
|
||||
|
@ -150,3 +150,5 @@ run result_error_access.cpp : : : $(CPP11) ;
|
||||
run result_swap.cpp : : : $(CPP11) <toolset>gcc-10:<cxxflags>"-Wno-maybe-uninitialized" ;
|
||||
run result_eq.cpp : : : $(CPP11) ;
|
||||
run result_range_for.cpp : : : $(CPP11) ;
|
||||
run result_value_construct2.cpp : : : $(CPP11) ;
|
||||
run result_error_construct2.cpp : : : $(CPP11) ;
|
||||
|
57
test/result_error_construct2.cpp
Normal file
57
test/result_error_construct2.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
// Copyright 2021 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>
|
||||
#include <string>
|
||||
|
||||
using namespace boost::system;
|
||||
|
||||
struct X
|
||||
{
|
||||
int a, b;
|
||||
};
|
||||
|
||||
result<X> fx1()
|
||||
{
|
||||
return {{ EINVAL, generic_category() }};
|
||||
}
|
||||
|
||||
struct Y
|
||||
{
|
||||
std::string v;
|
||||
};
|
||||
|
||||
struct E
|
||||
{
|
||||
int v;
|
||||
};
|
||||
|
||||
result<Y, E> fy1()
|
||||
{
|
||||
return {{ 42 }};
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
result<X> r = fx1();
|
||||
|
||||
BOOST_TEST( !r.has_value() );
|
||||
BOOST_TEST( r.has_error() );
|
||||
|
||||
BOOST_TEST_EQ( r.error(), std::error_code( EINVAL, generic_category() ) );
|
||||
}
|
||||
|
||||
{
|
||||
result<Y, E> r = fy1();
|
||||
|
||||
BOOST_TEST( !r.has_value() );
|
||||
BOOST_TEST( r.has_error() );
|
||||
|
||||
BOOST_TEST_EQ( r.error().v, 42 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
152
test/result_value_construct2.cpp
Normal file
152
test/result_value_construct2.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
// Copyright 2021 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>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace boost::system;
|
||||
|
||||
struct X
|
||||
{
|
||||
int a;
|
||||
};
|
||||
|
||||
struct Y
|
||||
{
|
||||
int a, b;
|
||||
};
|
||||
|
||||
struct E
|
||||
{
|
||||
std::string v;
|
||||
};
|
||||
|
||||
result<X> fx0()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
result<X> fx1()
|
||||
{
|
||||
return {{ 1 }};
|
||||
}
|
||||
|
||||
result<Y> fy0()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
result<Y> fy2()
|
||||
{
|
||||
return {{ 1, 2 }};
|
||||
}
|
||||
|
||||
result<X, E> fxe0()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
result<X, E> fxe1()
|
||||
{
|
||||
return {{ 1 }};
|
||||
}
|
||||
|
||||
result<std::vector<int>> fv0()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
result<std::vector<int>> fv1()
|
||||
{
|
||||
return {{ 1 }};
|
||||
}
|
||||
|
||||
result<std::vector<int>> fv2()
|
||||
{
|
||||
return {{ 1, 2 }};
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
result<X> r = fx0();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->a, 0 );
|
||||
}
|
||||
|
||||
{
|
||||
result<X> r = fx1();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->a, 1 );
|
||||
}
|
||||
|
||||
{
|
||||
result<Y> r = fy0();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->a, 0 );
|
||||
BOOST_TEST_EQ( r->b, 0 );
|
||||
}
|
||||
|
||||
{
|
||||
result<Y> r = fy2();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->a, 1 );
|
||||
BOOST_TEST_EQ( r->b, 2 );
|
||||
}
|
||||
|
||||
{
|
||||
result<X, E> r = fxe0();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->a, 0 );
|
||||
}
|
||||
|
||||
{
|
||||
result<X, E> r = fxe1();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->a, 1 );
|
||||
}
|
||||
|
||||
{
|
||||
result<std::vector<int>> r = fv0();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->size(), 0 );
|
||||
}
|
||||
|
||||
{
|
||||
result<std::vector<int>> r = fv1();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->size(), 1 );
|
||||
BOOST_TEST_EQ( r->at(0), 1 );
|
||||
}
|
||||
|
||||
{
|
||||
result<std::vector<int>> r = fv2();
|
||||
|
||||
BOOST_TEST( r.has_value() );
|
||||
|
||||
BOOST_TEST_EQ( r->size(), 2 );
|
||||
BOOST_TEST_EQ( r->at(0), 1 );
|
||||
BOOST_TEST_EQ( r->at(1), 2 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user