forked from boostorg/variant2
Merge branch 'develop'
This commit is contained in:
259
include/boost/variant2/outcome.hpp
Normal file
259
include/boost/variant2/outcome.hpp
Normal file
@ -0,0 +1,259 @@
|
||||
#ifndef BOOST_VARIANT2_OUTCOME_HPP_INCLUDED
|
||||
#define BOOST_VARIANT2_OUTCOME_HPP_INCLUDED
|
||||
|
||||
// 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
|
||||
|
||||
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
|
||||
#include <boost/variant2/variant.hpp>
|
||||
#endif
|
||||
#include <system_error>
|
||||
#include <exception>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
//
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace variant2
|
||||
{
|
||||
|
||||
enum class outcome_errc
|
||||
{
|
||||
not_initialized
|
||||
};
|
||||
|
||||
class outcome_error_category: public std::error_category
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char * name() const noexcept
|
||||
{
|
||||
return "boost::variant2::outcome";
|
||||
}
|
||||
|
||||
virtual std::string message( int e ) const
|
||||
{
|
||||
switch( e )
|
||||
{
|
||||
case (int)outcome_errc::not_initialized:
|
||||
|
||||
return "outcome<> not initialized";
|
||||
|
||||
default:
|
||||
|
||||
return "unknown outcome<> error";
|
||||
}
|
||||
}
|
||||
|
||||
static outcome_error_category const & instance()
|
||||
{
|
||||
static outcome_error_category cat;
|
||||
return cat;
|
||||
}
|
||||
};
|
||||
|
||||
std::error_code make_error_code( outcome_errc e )
|
||||
{
|
||||
return std::error_code( static_cast<int>( e ), outcome_error_category::instance() );
|
||||
}
|
||||
|
||||
template<class T> class outcome
|
||||
{
|
||||
private:
|
||||
|
||||
variant<T, std::error_code, std::exception_ptr> v_;
|
||||
|
||||
public:
|
||||
|
||||
// constructors
|
||||
|
||||
constexpr outcome() noexcept: v_( make_error_code( outcome_errc::not_initialized ) )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr outcome( T const& t ): v_( t )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr outcome( T&& t ): v_( std::move(t) )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr outcome( std::error_code const & ec ) noexcept: v_( ec )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr outcome( std::exception_ptr const & ep ) noexcept: v_( ep )
|
||||
{
|
||||
}
|
||||
|
||||
// queries
|
||||
|
||||
constexpr bool has_value() const noexcept
|
||||
{
|
||||
return v_.index() == 0;
|
||||
}
|
||||
|
||||
constexpr bool has_error() const noexcept
|
||||
{
|
||||
return v_.index() == 1;
|
||||
}
|
||||
|
||||
constexpr bool has_exception() const noexcept
|
||||
{
|
||||
return v_.index() == 2;
|
||||
}
|
||||
|
||||
constexpr explicit operator bool() const noexcept
|
||||
{
|
||||
return v_.index() == 0;
|
||||
}
|
||||
|
||||
// checked value access
|
||||
|
||||
constexpr T& value() &
|
||||
{
|
||||
if( has_value() )
|
||||
{
|
||||
return *get_if<0>(&v_);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::system_error( *get_if<1>(&v_) );
|
||||
}
|
||||
}
|
||||
|
||||
constexpr T const& value() const&
|
||||
{
|
||||
if( has_value() )
|
||||
{
|
||||
return *get_if<0>(&v_);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::system_error( *get_if<1>(&v_) );
|
||||
}
|
||||
}
|
||||
|
||||
constexpr T&& value() &&
|
||||
{
|
||||
return std::move( value() );
|
||||
}
|
||||
|
||||
constexpr T const&& value() const&&
|
||||
{
|
||||
return std::move( value() );
|
||||
}
|
||||
|
||||
// unchecked value access
|
||||
|
||||
T* operator->() noexcept
|
||||
{
|
||||
return get_if<0>(&v_);
|
||||
}
|
||||
|
||||
T const* operator->() const noexcept
|
||||
{
|
||||
return get_if<0>(&v_);
|
||||
}
|
||||
|
||||
T& operator*() & noexcept
|
||||
{
|
||||
T* p = get_if<0>(&v_);
|
||||
|
||||
assert( p != 0 );
|
||||
|
||||
return *p;
|
||||
}
|
||||
|
||||
T const& operator*() const & noexcept
|
||||
{
|
||||
T const* p = get_if<0>(&v_);
|
||||
|
||||
assert( p != 0 );
|
||||
|
||||
return *p;
|
||||
}
|
||||
|
||||
T&& operator*() && noexcept
|
||||
{
|
||||
return std::move(**this);
|
||||
}
|
||||
|
||||
T const&& operator*() const && noexcept
|
||||
{
|
||||
return std::move(**this);
|
||||
}
|
||||
|
||||
// error access
|
||||
|
||||
/*constexpr*/ std::error_code error() const noexcept
|
||||
{
|
||||
if( has_error() )
|
||||
{
|
||||
return *get_if<1>(&v_);
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::error_code();
|
||||
}
|
||||
}
|
||||
|
||||
// exception access
|
||||
|
||||
std::exception_ptr exception() const noexcept
|
||||
{
|
||||
if( has_exception() )
|
||||
{
|
||||
return *get_if<2>(&v_);
|
||||
}
|
||||
else if( has_value() )
|
||||
{
|
||||
return std::exception_ptr();
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::make_exception_ptr( std::system_error( *get_if<1>(&v_) ) );
|
||||
}
|
||||
}
|
||||
|
||||
// setters
|
||||
|
||||
void set_value( T const& t ) noexcept( std::is_nothrow_copy_constructible<T>::value )
|
||||
{
|
||||
v_.emplace<0>( t );
|
||||
}
|
||||
|
||||
void set_value( T&& t ) noexcept( std::is_nothrow_move_constructible<T>::value )
|
||||
{
|
||||
v_.emplace<0>( std::move( t ) );
|
||||
}
|
||||
|
||||
void set_error( std::error_code const & e ) noexcept
|
||||
{
|
||||
v_.emplace<1>( e );
|
||||
}
|
||||
|
||||
void set_exception( std::exception_ptr const & x ) noexcept
|
||||
{
|
||||
v_.emplace<2>( x );
|
||||
}
|
||||
|
||||
// swap
|
||||
|
||||
void swap( outcome& r ) noexcept( noexcept( v_.swap( r.v_ ) ) )
|
||||
{
|
||||
v_.swap( r.v_ );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace variant2
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_VARIANT2_OUTCOME_HPP_INCLUDED
|
226
include/boost/variant2/result.hpp
Normal file
226
include/boost/variant2/result.hpp
Normal file
@ -0,0 +1,226 @@
|
||||
#ifndef BOOST_VARIANT2_RESULT_HPP_INCLUDED
|
||||
#define BOOST_VARIANT2_RESULT_HPP_INCLUDED
|
||||
|
||||
// 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
|
||||
|
||||
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
|
||||
#include <boost/variant2/variant.hpp>
|
||||
#endif
|
||||
#include <system_error>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
//
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace variant2
|
||||
{
|
||||
|
||||
enum class result_errc
|
||||
{
|
||||
not_initialized
|
||||
};
|
||||
|
||||
class result_error_category: public std::error_category
|
||||
{
|
||||
public:
|
||||
|
||||
virtual const char * name() const noexcept
|
||||
{
|
||||
return "boost::variant2::result";
|
||||
}
|
||||
|
||||
virtual std::string message( int e ) const
|
||||
{
|
||||
switch( e )
|
||||
{
|
||||
case (int)result_errc::not_initialized:
|
||||
|
||||
return "result<> not initialized";
|
||||
|
||||
default:
|
||||
|
||||
return "unknown result<> error";
|
||||
}
|
||||
}
|
||||
|
||||
static result_error_category const & instance()
|
||||
{
|
||||
static result_error_category cat;
|
||||
return cat;
|
||||
}
|
||||
};
|
||||
|
||||
std::error_code make_error_code( result_errc e )
|
||||
{
|
||||
return std::error_code( static_cast<int>( e ), result_error_category::instance() );
|
||||
}
|
||||
|
||||
template<class T> class result
|
||||
{
|
||||
private:
|
||||
|
||||
variant<T, std::error_code> v_;
|
||||
|
||||
public:
|
||||
|
||||
// constructors
|
||||
|
||||
constexpr result() noexcept: v_( make_error_code( result_errc::not_initialized ) )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr result( T const& t ): v_( t )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr result( T&& t ): v_( std::move(t) )
|
||||
{
|
||||
}
|
||||
|
||||
constexpr result( std::error_code const & ec ) noexcept: v_( ec )
|
||||
{
|
||||
}
|
||||
|
||||
// queries
|
||||
|
||||
constexpr bool has_value() const noexcept
|
||||
{
|
||||
return v_.index() == 0;
|
||||
}
|
||||
|
||||
constexpr bool has_error() const noexcept
|
||||
{
|
||||
return v_.index() == 1;
|
||||
}
|
||||
|
||||
constexpr explicit operator bool() const noexcept
|
||||
{
|
||||
return v_.index() == 0;
|
||||
}
|
||||
|
||||
// checked value access
|
||||
|
||||
constexpr T& value() &
|
||||
{
|
||||
if( has_value() )
|
||||
{
|
||||
return *get_if<0>(&v_);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::system_error( *get_if<1>(&v_) );
|
||||
}
|
||||
}
|
||||
|
||||
constexpr T const& value() const&
|
||||
{
|
||||
if( has_value() )
|
||||
{
|
||||
return *get_if<0>(&v_);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::system_error( *get_if<1>(&v_) );
|
||||
}
|
||||
}
|
||||
|
||||
constexpr T&& value() &&
|
||||
{
|
||||
return std::move( value() );
|
||||
}
|
||||
|
||||
constexpr T const&& value() const&&
|
||||
{
|
||||
return std::move( value() );
|
||||
}
|
||||
|
||||
// unchecked value access
|
||||
|
||||
T* operator->() noexcept
|
||||
{
|
||||
return get_if<0>(&v_);
|
||||
}
|
||||
|
||||
T const* operator->() const noexcept
|
||||
{
|
||||
return get_if<0>(&v_);
|
||||
}
|
||||
|
||||
T& operator*() & noexcept
|
||||
{
|
||||
T* p = get_if<0>(&v_);
|
||||
|
||||
assert( p != 0 );
|
||||
|
||||
return *p;
|
||||
}
|
||||
|
||||
T const& operator*() const & noexcept
|
||||
{
|
||||
T const* p = get_if<0>(&v_);
|
||||
|
||||
assert( p != 0 );
|
||||
|
||||
return *p;
|
||||
}
|
||||
|
||||
T&& operator*() && noexcept
|
||||
{
|
||||
return std::move(**this);
|
||||
}
|
||||
|
||||
T const&& operator*() const && noexcept
|
||||
{
|
||||
return std::move(**this);
|
||||
}
|
||||
|
||||
// error access
|
||||
|
||||
/*constexpr*/ std::error_code error() const noexcept
|
||||
{
|
||||
if( has_error() )
|
||||
{
|
||||
return *get_if<1>(&v_);
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::error_code();
|
||||
}
|
||||
}
|
||||
|
||||
// setters
|
||||
|
||||
void set_value( T const& t ) noexcept( std::is_nothrow_copy_constructible<T>::value )
|
||||
{
|
||||
v_.emplace<0>( t );
|
||||
}
|
||||
|
||||
void set_value( T&& t ) noexcept( std::is_nothrow_move_constructible<T>::value )
|
||||
{
|
||||
v_.emplace<0>( std::move( t ) );
|
||||
}
|
||||
|
||||
void set_error( std::error_code const & e ) noexcept
|
||||
{
|
||||
v_.emplace<1>( e );
|
||||
}
|
||||
|
||||
// swap
|
||||
|
||||
void swap( result& r ) noexcept( noexcept( v_.swap( r.v_ ) ) )
|
||||
{
|
||||
v_.swap( r.v_ );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace variant2
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_VARIANT2_RESULT_HPP_INCLUDED
|
@ -1,14 +1,16 @@
|
||||
#ifndef BOOST_VARIANT2_VARIANT_HPP_INCLUDED
|
||||
#define BOOST_VARIANT2_VARIANT_HPP_INCLUDED
|
||||
|
||||
// Copyright 2015-2017 Peter Dimov.
|
||||
// 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
|
||||
|
||||
#ifndef BOOST_MP11_HPP_INCLUDED
|
||||
#include <boost/mp11.hpp>
|
||||
#endif
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <cstddef>
|
||||
@ -79,22 +81,22 @@ template<std::size_t I, class T> using variant_alternative_t = typename variant_
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<class I, class T, class Q> using var_alt_t = mp_invoke<Q, variant_alternative_t<I::value, T>>;
|
||||
template<class I, class T, class Q> using var_alt_impl = mp_invoke<Q, variant_alternative_t<I::value, T>>;
|
||||
} // namespace detail
|
||||
|
||||
template<std::size_t I, class T> struct variant_alternative
|
||||
{
|
||||
};
|
||||
|
||||
template<std::size_t I, class T> struct variant_alternative<I, T const>: mp_defer< variant2::detail::var_alt_t, mp_size_t<I>, T, mp_quote<std::add_const_t>>
|
||||
template<std::size_t I, class T> struct variant_alternative<I, T const>: mp_defer< variant2::detail::var_alt_impl, mp_size_t<I>, T, mp_quote<std::add_const_t>>
|
||||
{
|
||||
};
|
||||
|
||||
template<std::size_t I, class T> struct variant_alternative<I, T volatile>: mp_defer< variant2::detail::var_alt_t, mp_size_t<I>, T, mp_quote<std::add_volatile_t>>
|
||||
template<std::size_t I, class T> struct variant_alternative<I, T volatile>: mp_defer< variant2::detail::var_alt_impl, mp_size_t<I>, T, mp_quote<std::add_volatile_t>>
|
||||
{
|
||||
};
|
||||
|
||||
template<std::size_t I, class T> struct variant_alternative<I, T const volatile>: mp_defer< variant2::detail::var_alt_t, mp_size_t<I>, T, mp_quote<std::add_cv_t>>
|
||||
template<std::size_t I, class T> struct variant_alternative<I, T const volatile>: mp_defer< variant2::detail::var_alt_impl, mp_size_t<I>, T, mp_quote<std::add_cv_t>>
|
||||
{
|
||||
};
|
||||
|
||||
@ -1063,16 +1065,9 @@ template<class... T> constexpr bool operator>=( variant<T...> const & v, variant
|
||||
}
|
||||
|
||||
// visitation
|
||||
template<class F> constexpr auto visit( F&& f ) -> decltype(std::forward<F>(f)())
|
||||
{
|
||||
return std::forward<F>(f)();
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<class T> using remove_cv_ref = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
template<class F> struct Qret
|
||||
{
|
||||
template<class... T> using fn = decltype( std::declval<F>()( std::declval<T>()... ) );
|
||||
@ -1080,13 +1075,41 @@ template<class F> struct Qret
|
||||
|
||||
template<class L> using front_if_same = mp_if<mp_apply<mp_same, L>, mp_front<L>>;
|
||||
|
||||
template<class F, class... V> using Vret = front_if_same<mp_product_q<Qret<F>, remove_cv_ref<V>...>>;
|
||||
template<class V> using var_size = variant_size<std::remove_reference_t<V>>;
|
||||
|
||||
template<class V, class N, class T = variant_alternative_t<N::value, std::remove_reference_t<V>>> using apply_cv_ref_ = mp_if<std::is_reference<V>, T&, T>;
|
||||
|
||||
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
|
||||
|
||||
template<class V> struct apply_cv_ref_impl
|
||||
{
|
||||
template<class T> using _f = apply_cv_ref_<V, T>;
|
||||
|
||||
using L = mp_iota<var_size<V>>;
|
||||
|
||||
using type = mp_transform<_f, L>;
|
||||
};
|
||||
|
||||
template<class V> using apply_cv_ref = typename apply_cv_ref_impl<V>::type;
|
||||
|
||||
#else
|
||||
|
||||
template<class V> using apply_cv_ref = mp_transform_q<mp_bind_front<apply_cv_ref_, V>, mp_iota<var_size<V>>>;
|
||||
|
||||
#endif
|
||||
|
||||
template<class F, class... V> using Vret = front_if_same<mp_product_q<Qret<F>, apply_cv_ref<V>...>>;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class F> constexpr auto visit( F&& f ) -> decltype(std::forward<F>(f)())
|
||||
{
|
||||
return std::forward<F>(f)();
|
||||
}
|
||||
|
||||
template<class F, class V1> constexpr auto visit( F&& f, V1&& v1 ) -> variant2::detail::Vret<F, V1>
|
||||
{
|
||||
return mp_for_index<mp_size<variant2::detail::remove_cv_ref<V1>>>( v1.index(), [&]( auto I ){
|
||||
return mp_for_index<variant2::detail::var_size<V1>>( v1.index(), [&]( auto I ){
|
||||
|
||||
return std::forward<F>(f)( get<I>( std::forward<V1>(v1) ) );
|
||||
|
||||
@ -1097,7 +1120,7 @@ template<class F, class V1> constexpr auto visit( F&& f, V1&& v1 ) -> variant2::
|
||||
|
||||
template<class F, class V1, class V2> constexpr auto visit( F&& f, V1&& v1, V2&& v2 ) -> variant2::detail::Vret<F, V1, V2>
|
||||
{
|
||||
return mp_for_index<mp_size<variant2::detail::remove_cv_ref<V1>>>( v1.index(), [&]( auto I ){
|
||||
return mp_for_index<variant2::detail::var_size<V1>>( v1.index(), [&]( auto I ){
|
||||
|
||||
auto f2 = [&]( auto&&... a ){ return std::forward<F>(f)( get<I.value>( std::forward<V1>(v1) ), std::forward<decltype(a)>(a)... ); };
|
||||
return visit( f2, std::forward<V2>(v2) );
|
||||
@ -1107,7 +1130,7 @@ template<class F, class V1, class V2> constexpr auto visit( F&& f, V1&& v1, V2&&
|
||||
|
||||
template<class F, class V1, class V2, class V3> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3 ) -> variant2::detail::Vret<F, V1, V2, V3>
|
||||
{
|
||||
return mp_for_index<mp_size<variant2::detail::remove_cv_ref<V1>>>( v1.index(), [&]( auto I ){
|
||||
return mp_for_index<variant2::detail::var_size<V1>>( v1.index(), [&]( auto I ){
|
||||
|
||||
auto f2 = [&]( auto&&... a ){ return std::forward<F>(f)( get<I.value>( std::forward<V1>(v1) ), std::forward<decltype(a)>(a)... ); };
|
||||
return visit( f2, std::forward<V2>(v2), std::forward<V3>(v3) );
|
||||
@ -1117,7 +1140,7 @@ template<class F, class V1, class V2, class V3> constexpr auto visit( F&& f, V1&
|
||||
|
||||
template<class F, class V1, class V2, class V3, class V4> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V3&& v3, V4&& v4 ) -> variant2::detail::Vret<F, V1, V2, V3, V4>
|
||||
{
|
||||
return mp_for_index<mp_size<variant2::detail::remove_cv_ref<V1>>>( v1.index(), [&]( auto I ){
|
||||
return mp_for_index<variant2::detail::var_size<V1>>( v1.index(), [&]( auto I ){
|
||||
|
||||
auto f2 = [&]( auto&&... a ){ return std::forward<F>(f)( get<I.value>( std::forward<V1>(v1) ), std::forward<decltype(a)>(a)... ); };
|
||||
return visit( f2, std::forward<V2>(v2), std::forward<V3>(v3), std::forward<V4>(v4) );
|
||||
@ -1129,7 +1152,7 @@ template<class F, class V1, class V2, class V3, class V4> constexpr auto visit(
|
||||
|
||||
template<class F, class V1, class V2, class... V> constexpr auto visit( F&& f, V1&& v1, V2&& v2, V&&... v ) -> variant2::detail::Vret<F, V1, V2, V...>
|
||||
{
|
||||
return mp_for_index<mp_size<variant2::detail::remove_cv_ref<V1>>>( v1.index(), [&]( auto I ){
|
||||
return mp_for_index<variant2::detail::var_size<V1>>( v1.index(), [&]( auto I ){
|
||||
|
||||
auto f2 = [&]( auto&&... a ){ return std::forward<F>(f)( get<I.value>( std::forward<V1>(v1) ), std::forward<decltype(a)>(a)... ); };
|
||||
return visit( f2, std::forward<V2>(v2), std::forward<V>(v)... );
|
||||
|
@ -38,3 +38,4 @@ run variant_swap.cpp : : : $(REQ) ;
|
||||
run variant_eq_ne.cpp : : : $(REQ) ;
|
||||
run variant_destroy.cpp : : : $(REQ) ;
|
||||
run variant_visit.cpp : : : $(REQ) ;
|
||||
run variant_lt_gt.cpp : : : $(REQ) ;
|
||||
|
85
test/variant_lt_gt.cpp
Normal file
85
test/variant_lt_gt.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
|
||||
// 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;
|
||||
|
||||
struct X
|
||||
{
|
||||
};
|
||||
|
||||
inline bool operator<( X const&, X const& ) { return false; }
|
||||
inline bool operator>( X const&, X const& ) { return false; }
|
||||
inline bool operator<=( X const&, X const& ) { return false; }
|
||||
inline bool operator>=( X const&, X const& ) { return false; }
|
||||
|
||||
#define TEST_EQ( v1, v2 ) \
|
||||
BOOST_TEST_NOT( v1 < v2 ); \
|
||||
BOOST_TEST_NOT( v1 > v2 ); \
|
||||
BOOST_TEST( v1 <= v2 ); \
|
||||
BOOST_TEST( v1 >= v2 );
|
||||
|
||||
#define TEST_LE( v1, v3 ) \
|
||||
BOOST_TEST( v1 < v3 ); \
|
||||
BOOST_TEST( v3 > v1 ); \
|
||||
BOOST_TEST_NOT( v1 > v3 ); \
|
||||
BOOST_TEST_NOT( v3 < v1 ); \
|
||||
BOOST_TEST( v1 <= v3 ); \
|
||||
BOOST_TEST( v3 >= v1 ); \
|
||||
BOOST_TEST_NOT( v1 >= v3 ); \
|
||||
BOOST_TEST_NOT( v3 <= v1 );
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
variant<int> v1, v2, v3( 1 ), v4( 1 );
|
||||
|
||||
TEST_EQ( v1, v2 )
|
||||
TEST_LE( v1, v3 )
|
||||
TEST_EQ( v3, v4 )
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, float> v1, v2, v3( 1 ), v4( 1 ), v5( 3.14f ), v6( 3.14f );
|
||||
|
||||
TEST_EQ( v1, v2 )
|
||||
TEST_LE( v1, v3 )
|
||||
TEST_EQ( v3, v4 )
|
||||
TEST_LE( v1, v5 )
|
||||
TEST_LE( v3, v5 )
|
||||
TEST_EQ( v5, v6 )
|
||||
}
|
||||
|
||||
{
|
||||
variant<int, int, float> v1, v2, v3( in_place_index<1> ), v4( in_place_index<1> ), v5( 3.14f ), v6( 3.14f );
|
||||
|
||||
TEST_EQ( v1, v2 )
|
||||
TEST_LE( v1, v3 )
|
||||
TEST_EQ( v3, v4 )
|
||||
TEST_LE( v1, v5 )
|
||||
TEST_LE( v3, v5 )
|
||||
TEST_EQ( v5, v6 )
|
||||
}
|
||||
|
||||
{
|
||||
variant<X> v1, v2;
|
||||
|
||||
BOOST_TEST_NOT( v1 < v2 );
|
||||
BOOST_TEST_NOT( v1 > v2 );
|
||||
BOOST_TEST_NOT( v1 <= v2 );
|
||||
BOOST_TEST_NOT( v1 >= v2 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/variant2/variant.hpp>
|
||||
#include <boost/mp11.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/core/lightweight_test_trait.hpp>
|
||||
#include <type_traits>
|
||||
@ -15,6 +16,19 @@
|
||||
#include <cstdio>
|
||||
|
||||
using namespace boost::variant2;
|
||||
using boost::mp11::mp_size_t;
|
||||
|
||||
struct X
|
||||
{
|
||||
};
|
||||
|
||||
struct F
|
||||
{
|
||||
mp_size_t<1> operator()( X& ) const;
|
||||
mp_size_t<2> operator()( X const& ) const;
|
||||
mp_size_t<3> operator()( X&& ) const;
|
||||
mp_size_t<4> operator()( X const&& ) const;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
@ -84,7 +98,17 @@ int main()
|
||||
BOOST_TEST_EQ( (visit( []( auto x1, auto x2, auto x3, auto x4 ){ return (long long)(x1 * 100) * 100000000 + (long long)(x2 * 100) * 100000 + (long long)(x3 * 10000) + (int)x4; }, v1, v2, v3, v4 )), 10031462800 + 'A' );
|
||||
|
||||
visit( []( auto x1, auto x2, auto x3, auto x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, v1, v2, v3, v4 );
|
||||
visit( []( auto x1, auto x2, auto x3, auto x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, std::move(v1), std::move(v2), std::move(v3), std::move(v4) );
|
||||
visit( []( auto x1, auto x2, auto x3, auto x4 ){ BOOST_TEST_EQ( x1, 1 ); BOOST_TEST_EQ( x2, 3.14f ); BOOST_TEST_EQ( x3, 6.28 ); BOOST_TEST_EQ( x4, 'A' ); }, std::move(v1), std::move(v2), std::move(v3), std::move(v4) );
|
||||
}
|
||||
|
||||
{
|
||||
variant<X> v;
|
||||
variant<X> const cv;
|
||||
|
||||
BOOST_TEST_EQ( decltype(visit(F{}, v))::value, 1 );
|
||||
BOOST_TEST_EQ( decltype(visit(F{}, cv))::value, 2 );
|
||||
BOOST_TEST_EQ( decltype(visit(F{}, std::move(v)))::value, 3 );
|
||||
BOOST_TEST_EQ( decltype(visit(F{}, std::move(cv)))::value, 4 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
|
Reference in New Issue
Block a user