Merge branch 'develop'

This commit is contained in:
Peter Dimov
2017-06-03 00:46:39 +03:00
7 changed files with 450 additions and 19 deletions

View File

@@ -78,17 +78,10 @@ private:
public:
explicit bad_expected_access( E const& e )
noexcept( std::is_nothrow_copy_constructible<E>::value )
: bad_expected_access<void>( "bad_expected_access<" + boost::core::demangle( typeid(E).name() ) + ">" + variant2::detail::add_value( e ) ), e_( e )
{
}
explicit bad_expected_access( E&& e )
noexcept( std::is_nothrow_move_constructible<E>::value )
: bad_expected_access<void>( "bad_expected_access<" + boost::core::demangle( typeid(E).name() ) + ">" + variant2::detail::add_value( e ) ), e_( std::move(e) )
{
}
E error() const
{
return e_;
@@ -377,7 +370,7 @@ public:
return mp_with_index<mp_size<expected>>( v_.index(), [&]( auto I ) {
return _remap_error<R>( I, f, get<I>(v_) );
return this->_remap_error<R>( I, f, get<I>(v_) );
});
}
@@ -390,7 +383,7 @@ public:
return mp_with_index<mp_size<expected>>( v_.index(), [&]( auto I ) {
return _remap_error<R>( I, f, get<I>(v_) );
return this->_remap_error<R>( I, f, get<I>(v_) );
});
}
@@ -416,6 +409,18 @@ public:
return unexpected();
}
}
template<class F> then_result<F, T const&> operator>>( F && f ) const
{
if( has_value() )
{
return std::forward<F>(f)( **this );
}
else
{
return unexpected();
}
}
};
template<class T, class... E> inline constexpr bool operator==( expected<T, E...> const & x1, expected<T, E...> const & x2 )

View File

@@ -112,7 +112,7 @@ template<class U, class... T> constexpr bool holds_alternative( variant<T...> co
return v.index() == mp_find<variant<T...>, U>::value;
}
// get
// get (index)
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& get(variant<T...>& v)
{
@@ -178,7 +178,7 @@ template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T
#endif
}
// get
// get (type)
template<class U, class... T> constexpr U& get(variant<T...>& v)
{
@@ -253,13 +253,13 @@ template<class U, class... T> constexpr U const&& get(variant<T...> const&& v)
template<std::size_t I, class... T> constexpr std::add_pointer_t<variant_alternative_t<I, variant<T...>>> get_if(variant<T...>* v) noexcept
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
template<std::size_t I, class... T> constexpr std::add_pointer_t<const variant_alternative_t<I, variant<T...>>> get_if(variant<T...> const * v) noexcept
{
static_assert( I < sizeof...(T), "Index out of bounds" );
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
template<class U, class... T> constexpr std::add_pointer_t<U> get_if(variant<T...>* v) noexcept
@@ -267,7 +267,7 @@ template<class U, class... T> constexpr std::add_pointer_t<U> get_if(variant<T..
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
template<class U, class... T> constexpr std::add_pointer_t<U const> get_if(variant<T...> const * v) noexcept
@@ -275,7 +275,7 @@ template<class U, class... T> constexpr std::add_pointer_t<U const> get_if(varia
static_assert( mp_count<variant<T...>, U>::value == 1, "The type must occur exactly once in the list of variant alternatives" );
constexpr auto I = mp_find<variant<T...>, U>::value;
return v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
return v && v->index() == I? &v->_get_impl( mp_size_t<I>() ): 0;
}
//
@@ -1064,7 +1064,7 @@ public:
using J = mp_find<mp_list<U...>, mp_at_c<mp_list<T...>, I>>;
return _subset_impl<U...>( J{}, this->_get_impl( I ) );
return this->_subset_impl<U...>( J{}, this->_get_impl( I ) );
});
}
@@ -1077,7 +1077,7 @@ public:
using J = mp_find<mp_list<U...>, mp_at_c<mp_list<T...>, I>>;
return _subset_impl<U...>( J{}, this->_get_impl( I ) );
return this->_subset_impl<U...>( J{}, this->_get_impl( I ) );
});
}
@@ -1090,7 +1090,7 @@ public:
using J = mp_find<mp_list<U...>, mp_at_c<mp_list<T...>, I>>;
return _subset_impl<U...>( J{}, std::move( this->_get_impl( I ) ) );
return this->_subset_impl<U...>( J{}, std::move( this->_get_impl( I ) ) );
});
}
@@ -1103,7 +1103,7 @@ public:
using J = mp_find<mp_list<U...>, mp_at_c<mp_list<T...>, I>>;
return _subset_impl<U...>( J{}, std::move( this->_get_impl( I ) ) );
return this->_subset_impl<U...>( J{}, std::move( this->_get_impl( I ) ) );
});
}

View File

@@ -39,3 +39,5 @@ run variant_eq_ne.cpp : : : $(REQ) ;
run variant_destroy.cpp : : : $(REQ) ;
run variant_visit.cpp : : : $(REQ) ;
run variant_lt_gt.cpp : : : $(REQ) ;
run variant_convert_construct.cpp : : : $(REQ) ;
run variant_subset.cpp : : : $(REQ) ;

View File

@@ -0,0 +1,175 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
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_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
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_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v( 1 );
variant<int, float> v2( v );
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v ), get<int>( v2 ) );
variant<int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
}
{
variant<int> const v( 1 );
variant<int, float> v2( v );
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v ), get<int>( v2 ) );
variant<int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
}
{
variant<int const> v( 1 );
variant<int const, float> v2( v );
BOOST_TEST( holds_alternative<int const>( v2 ) );
BOOST_TEST_EQ( get<int const>( v ), get<int const>( v2 ) );
variant<int const, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int const>( v3 ) );
BOOST_TEST_EQ( get<int const>( v2 ), get<int const>( v3 ) );
}
{
variant<int const> const v( 1 );
variant<int const, float> v2( v );
BOOST_TEST( holds_alternative<int const>( v2 ) );
BOOST_TEST_EQ( get<int const>( v ), get<int const>( v2 ) );
variant<int const, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<int const>( v3 ) );
BOOST_TEST_EQ( get<int const>( v2 ), get<int const>( v3 ) );
}
{
variant<float> v( 3.14f );
variant<int, float> v2( v );
BOOST_TEST( holds_alternative<float>( v2 ) );
BOOST_TEST_EQ( get<float>( v ), get<float>( v2 ) );
variant<int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<float>( v3 ) );
BOOST_TEST_EQ( get<float>( v2 ), get<float>( v3 ) );
}
{
variant<float> v( 3.15f );
variant<int, int, float> v2( v );
BOOST_TEST( holds_alternative<float>( v2 ) );
BOOST_TEST_EQ( get<float>( v ), get<float>( v2 ) );
variant<int, int, float> v3( std::move(v) );
BOOST_TEST( holds_alternative<float>( v3 ) );
BOOST_TEST_EQ( get<float>( v2 ), get<float>( v3 ) );
}
{
variant<float, std::string> v( "s1" );
variant<int, int, float, std::string> v2( v );
BOOST_TEST( holds_alternative<std::string>( v2 ) );
BOOST_TEST_EQ( get<std::string>( v ), get<std::string>( v2 ) );
variant<int, int, float, std::string> v3( std::move(v) );
BOOST_TEST( holds_alternative<std::string>( v3 ) );
BOOST_TEST_EQ( get<std::string>( v2 ), get<std::string>( v3 ) );
}
{
variant<X1, X2> v{ X1{1} };
variant<int, int, float, float, X1, X2> v2( v );
BOOST_TEST( holds_alternative<X1>( v2 ) );
BOOST_TEST_EQ( get<X1>( v ).v, get<X1>( v2 ).v );
variant<int, int, float, float, X1, X2> v3( std::move(v) );
BOOST_TEST( holds_alternative<X1>( v3 ) );
BOOST_TEST_EQ( get<X1>( v2 ).v, get<X1>( v3 ).v );
}
return boost::report_errors();
}

View File

@@ -196,5 +196,55 @@ int main()
BOOST_TEST_EQ( get<2>(std::move(v)), 3.14f );
}
{
variant<int> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, float> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, float> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const, float volatile> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int const, float volatile> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, int, float, float> * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
{
variant<int, int, float, float> const * p = 0;
BOOST_TEST_EQ( get_if<0>(p), nullptr );
}
return boost::report_errors();
}

View File

@@ -162,5 +162,49 @@ int main()
BOOST_TEST_EQ( get_if<float>(&v), &get<float>(v) );
}
{
variant<int> * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
}
{
variant<int const> * p = 0;
BOOST_TEST_EQ( get_if<int const>(p), nullptr );
}
{
variant<int> const * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
}
{
variant<int const> const * p = 0;
BOOST_TEST_EQ( get_if<int const>(p), nullptr );
}
{
variant<int, float> * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, float> const * p = 0;
BOOST_TEST_EQ( get_if<int>(p), nullptr );
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, int, float> * p = 0;
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
{
variant<int, int, float> const * p = 0;
BOOST_TEST_EQ( get_if<float>(p), nullptr );
}
return boost::report_errors();
}

155
test/variant_subset.cpp Normal file
View File

@@ -0,0 +1,155 @@
// Copyright 2017 Peter Dimov.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
struct X1
{
int v;
X1(): v(0) {}
explicit X1(int v): v(v) {}
X1(X1 const& r): v(r.v) {}
X1(X1&& r): v(r.v) {}
X1& operator=( X1 const& r ) { v = r.v; return *this; }
X1& operator=( X1&& r ) { v = r.v; return *this; }
};
inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; }
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_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::value );
struct X2
{
int v;
X2(): v(0) {}
explicit X2(int v): v(v) {}
X2(X2 const& r): v(r.v) {}
X2(X2&& r): v(r.v) {}
X2& operator=( X2 const& r ) { v = r.v; return *this; }
X2& operator=( X2&& r ) { v = r.v; return *this; }
};
inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; }
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_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int, float> v1( 1 );
variant<int> v2 = v1.subset<int>();
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v1 ), get<int>( v2 ) );
BOOST_TEST_THROWS( v1.subset<float>(), bad_variant_access );
variant<int> v3 = std::move(v1).subset<int>();
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
BOOST_TEST_THROWS( std::move(v1).subset<float>(), bad_variant_access );
}
{
variant<int, float> const v1( 1 );
variant<int> v2 = v1.subset<int>();
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v1 ), get<int>( v2 ) );
BOOST_TEST_THROWS( v1.subset<float>(), bad_variant_access );
variant<int> v3 = std::move(v1).subset<int>();
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
BOOST_TEST_THROWS( std::move(v1).subset<float>(), bad_variant_access );
}
{
variant<int, float> v1( 1 );
variant<int, float> v2 = v1.subset<int, float>();
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v1 ), get<int>( v2 ) );
variant<int, float> v3 = std::move(v1).subset<int, float>();
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
}
{
variant<int, float> v1( 1 );
variant<float, int> v2 = v1.subset<float, int>();
BOOST_TEST( holds_alternative<int>( v2 ) );
BOOST_TEST_EQ( get<int>( v1 ), get<int>( v2 ) );
variant<float, int> v3 = std::move(v1).subset<float, int>();
BOOST_TEST( holds_alternative<int>( v3 ) );
BOOST_TEST_EQ( get<int>( v2 ), get<int>( v3 ) );
}
{
variant<int, float, std::string> v1( "s1" );
variant<int, std::string> v2 = v1.subset<int, std::string>();
BOOST_TEST( holds_alternative<std::string>( v2 ) );
BOOST_TEST_EQ( get<std::string>( v1 ), get<std::string>( v2 ) );
variant<float, std::string> v3 = std::move(v1).subset<float, std::string>();
BOOST_TEST( holds_alternative<std::string>( v3 ) );
BOOST_TEST_EQ( get<std::string>( v2 ), get<std::string>( v3 ) );
}
{
variant<int, int, float, float, X1, X2> v1{ X1{1} };
variant<X1, X2> v2 = v1.subset<X1, X2>();
BOOST_TEST( holds_alternative<X1>( v2 ) );
BOOST_TEST_EQ( get<X1>( v1 ).v, get<X1>( v2 ).v );
variant<X1, X2> v3 = std::move( v1 ).subset<X1, X2>();
BOOST_TEST( holds_alternative<X1>( v3 ) );
BOOST_TEST_EQ( get<X1>( v2 ).v, get<X1>( v3 ).v );
}
return boost::report_errors();
}