Remove valueless; monostate is enough

This commit is contained in:
Peter Dimov
2018-10-18 03:32:57 +03:00
parent 19d48d9d8b
commit 46ee0af110
2 changed files with 174 additions and 162 deletions

View File

@ -58,19 +58,6 @@ constexpr bool operator>=(monostate, monostate) noexcept { return true; }
constexpr bool operator==(monostate, monostate) noexcept { return true; }
constexpr bool operator!=(monostate, monostate) noexcept { return false; }
// valueless
struct valueless
{
};
constexpr bool operator<(valueless, valueless) noexcept { return false; }
constexpr bool operator>(valueless, valueless) noexcept { return false; }
constexpr bool operator<=(valueless, valueless) noexcept { return true; }
constexpr bool operator>=(valueless, valueless) noexcept { return true; }
constexpr bool operator==(valueless, valueless) noexcept { return true; }
constexpr bool operator!=(valueless, valueless) noexcept { return false; }
// variant forward declaration
template<class... T> class variant;
@ -459,7 +446,8 @@ template<class U, class... T> using resolve_overload_index = mp_find<mp_list<T..
// variant_base
template<class... T> using can_be_valueless = std::is_same<mp_first<mp_list<T...>>, valueless>;
template<class... T> using can_be_valueless = mp_any<std::is_same<T, monostate>..., std::is_nothrow_default_constructible<T>...>;
template<class... T> using valueless_index = mp_if<mp_contains<mp_list<T...>, monostate>, mp_find<mp_list<T...>, monostate>, mp_find_if<mp_list<T...>, std::is_nothrow_default_constructible>>;
template<bool is_trivially_destructible, bool is_single_buffered, class... T> struct variant_base_impl; // trivially destructible, single buffered
template<class... T> using variant_base = variant_base_impl<mp_all<std::is_trivially_destructible<T>...>::value, mp_any<mp_all<std::is_nothrow_move_constructible<T>...>, can_be_valueless<T...>>::value, T...>;
@ -519,9 +507,11 @@ template<class... T> struct variant_base_impl<true, true, T...>
template<std::size_t J, class U, class... A> void emplace_impl( mp_false, mp_false, A&&... a )
{
if( can_be_valueless<T...>::value ) // T0 == valueless
if( can_be_valueless<T...>::value )
{
std::size_t const K = 0;
std::size_t const K = valueless_index<T...>::value;
assert( K < sizeof...(T) );
try
{
@ -681,9 +671,11 @@ template<class... T> struct variant_base_impl<false, true, T...>
st1_.emplace( mp_size_t<J>(), std::forward<A>(a)... );
ix_ = J;
}
else if( can_be_valueless<T...>::value ) // T0 == valueless
else if( can_be_valueless<T...>::value )
{
std::size_t const K = 0;
std::size_t const K = valueless_index<T...>::value;
assert( K < sizeof...(T) );
_destroy();

View File

@ -17,217 +17,237 @@ using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
enum E1
{
e1
};
//
enum E2
{
e2
};
enum E1 { e1 };
enum E1x { e1x };
struct X1
{
X1() = default;
X1( E2 ) { throw std::runtime_error( "X1(E2)" ); }
X1( E1 ) noexcept {}
X1( E1x ) { throw std::runtime_error( "X1(E1x)" ); }
};
STATIC_ASSERT( std::is_nothrow_default_constructible<X1>::value );
STATIC_ASSERT( std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_constructible<X1, E2>::value );
STATIC_ASSERT( std::is_trivially_destructible<X1>::value );
STATIC_ASSERT( std::is_trivially_move_assignable<X1>::value );
STATIC_ASSERT( std::is_nothrow_constructible<X1, E1>::value );
STATIC_ASSERT( !std::is_nothrow_constructible<X1, E1x>::value );
enum E2 { e2 };
enum E2x { e2x };
struct X2
{
X2() = default;
X2();
~X2();
X2( E2 ) { throw std::runtime_error( "X1(E2)" ); }
X2( E2 ) noexcept {}
X2( E2x ) { throw std::runtime_error( "X2(E2x)" ); }
};
X2::X2() {}
X2::~X2() {}
STATIC_ASSERT( !std::is_nothrow_default_constructible<X2>::value );
STATIC_ASSERT( std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_constructible<X2, E2>::value );
STATIC_ASSERT( !std::is_trivially_destructible<X2>::value );
STATIC_ASSERT( std::is_nothrow_constructible<X2, E2>::value );
STATIC_ASSERT( !std::is_nothrow_constructible<X2, E2x>::value );
enum E3 { e3 };
enum E3x { e3x };
struct X3
{
X3() = default;
X3();
X3( X3 const& ) {}
X3( X3&& ) {}
X3( E3 ) noexcept {}
X3( E3x ) { throw std::runtime_error( "X3(E3x)" ); }
X3& operator=( X3 const& ) = default;
X3& operator=( X3&& ) = default;
};
X3::X3() {}
STATIC_ASSERT( !std::is_nothrow_default_constructible<X3>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X3>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X3>::value );
STATIC_ASSERT( std::is_trivially_destructible<X3>::value );
STATIC_ASSERT( std::is_trivially_move_assignable<X3>::value );
STATIC_ASSERT( std::is_nothrow_constructible<X3, E3>::value );
STATIC_ASSERT( !std::is_nothrow_constructible<X3, E3x>::value );
//
STATIC_ASSERT( std::is_nothrow_default_constructible<monostate>::value );
STATIC_ASSERT( std::is_nothrow_copy_constructible<monostate>::value );
STATIC_ASSERT( std::is_nothrow_move_constructible<monostate>::value );
STATIC_ASSERT( std::is_trivially_destructible<monostate>::value );
//
int main()
{
{
variant<monostate, E1, X1> v;
variant<X2, X1> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
v = e1x;
BOOST_ERROR( "`v = e1x;` failed to throw" );
}
catch( std::exception const& )
{
// basic guarantee; X1 is nothrow default-constructible
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
}
}
{
variant<X3, E1, X1> v;
v = e1;
variant<X1, X2> v( e2 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
}
catch( std::exception const& )
{
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
}
}
{
variant<valueless, E1, X1> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
}
catch( std::exception const& )
{
// all types are trivial, so we get a constexpr path
// and v.index() stays 1
BOOST_TEST( v.index() == 0 || v.index() == 1 );
if( v.index() == 1 )
{
BOOST_TEST_EQ( get<1>(v), e1 );
}
}
}
{
variant<monostate, E1, X1, valueless> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
try
{
v = e2;
BOOST_ERROR( "`v = e2` failed to throw" );
}
catch( std::exception const& )
{
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
}
}
{
variant<monostate, E1, X2> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
}
catch( std::exception const& )
{
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
}
}
{
variant<X3, E1, X2> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
}
catch( std::exception const& )
{
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
}
}
{
variant<valueless, E1, X2> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
v = e1x;
BOOST_ERROR( "`v = e1x;` failed to throw" );
}
catch( std::exception const& )
{
// basic guarantee; X1 is nothrow default-constructible
BOOST_TEST_EQ( v.index(), 0 );
}
}
{
variant<monostate, E1, X2, valueless> v;
variant<X2, X1, monostate> v;
v = e1;
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e2;
BOOST_ERROR( "`v = e2;` failed to throw" );
v = e1x;
BOOST_ERROR( "`v = e1x;` failed to throw" );
}
catch( std::exception const& )
{
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), e1 );
// basic guarantee; monostate
BOOST_TEST_EQ( v.index(), 2 );
}
}
{
variant<X1, X2, monostate> v( e2 );
BOOST_TEST_EQ( v.index(), 1 );
try
{
v = e1x;
BOOST_ERROR( "`v = e1x;` failed to throw" );
}
catch( std::exception const& )
{
// basic guarantee; monostate
BOOST_TEST_EQ( v.index(), 2 );
}
}
{
variant<X2, X3, X1> v;
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e3x;
BOOST_ERROR( "`v = e3x;` failed to throw" );
}
catch( std::exception const& )
{
// basic guarantee; X1 is nothrow default-constructible
BOOST_TEST_EQ( v.index(), 2 );
}
}
{
variant<X2, X3, X1, monostate> v;
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e3x;
BOOST_ERROR( "`v = e3x;` failed to throw" );
}
catch( std::exception const& )
{
// basic guarantee; monostate
BOOST_TEST_EQ( v.index(), 3 );
}
}
{
variant<X2, X3> v;
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e3x;
BOOST_ERROR( "`v = e3x;` failed to throw" );
}
catch( std::exception const& )
{
// double buffered, no change
BOOST_TEST_EQ( v.index(), 0 );
}
}
{
variant<X3, X1> v;
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e1x;
BOOST_ERROR( "`v = e1x;` failed to throw" );
}
catch( std::exception const& )
{
// all trivially destructible and move-assignable, no change
BOOST_TEST_EQ( v.index(), 0 );
}
}
{
variant<X3, X1, monostate> v;
BOOST_TEST_EQ( v.index(), 0 );
try
{
v = e1x;
BOOST_ERROR( "`v = e1x;` failed to throw" );
}
catch( std::exception const& )
{
// all trivially destructible and move-assignable, no change
BOOST_TEST_EQ( v.index(), 0 );
}
}