Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions include/boost/variant2/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,22 +773,60 @@ template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, c

// resolve_overload_*

template<class T> using remove_cv_ref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

struct narrowing_check_impl
{
template<class T>
static auto check(T (&&)[1]) -> mp11::mp_identity<T>;

template<class T, class U>
using fn = decltype(check<T>({ std::declval<U>() }));
};

template<class T, class U> using narrowing_check = mp11::mp_if<std::is_arithmetic<T>, mp11::mp_identity<T>, mp11::mp_invoke_q<narrowing_check_impl, T, U>>;

template<class... T> struct overload;

template<> struct overload<>
{
void operator()() const;
};

template<class T1, class... T> struct overload<T1, T...>: overload<T...>
template<class T1, class... T> struct overload<T1, T...> : overload<T...>
{
using overload<T...>::operator();
template<class U> auto operator()(T1, U&&) const -> narrowing_check<T1, U>;
};

template<class... T> struct overload<bool, T...> : overload<T...>
{
using overload<T...>::operator();
template<class U> auto operator()(bool, U&&) const -> mp11::mp_if<std::is_same<remove_cv_ref_t<U>, bool>, mp11::mp_identity<bool>>;
};

template<class... T> struct overload<const bool, T...> : overload<T...>
{
using overload<T...>::operator();
mp11::mp_identity<T1> operator()(T1) const;
template<class U> auto operator()(bool, U&&) const -> mp11::mp_if<std::is_same<remove_cv_ref_t<U>, bool>, mp11::mp_identity<const bool>>;
};

template<class... T> struct overload<volatile bool, T...> : overload<T...>
{
using overload<T...>::operator();
template<class U> auto operator()(bool, U&&) const -> mp11::mp_if<std::is_same<remove_cv_ref_t<U>, bool>, mp11::mp_identity<volatile bool>>;
};

template<class... T> struct overload<const volatile bool, T...> : overload<T...>
{
using overload<T...>::operator();
template<class U> auto operator()(bool, U&&) const -> mp11::mp_if<std::is_same<remove_cv_ref_t<U>, bool>, mp11::mp_identity<const volatile bool>>;
};


#if BOOST_WORKAROUND( BOOST_MSVC, < 1930 )

template<class U, class... T> using resolve_overload_type_ = decltype( overload<T...>()(std::declval<U>()) );
template<class U, class... T> using resolve_overload_type_ = decltype( overload<T...>()(std::declval<U>(), std::declval<U>()) );

template<class U, class... T> struct resolve_overload_type_impl: mp11::mp_defer< resolve_overload_type_, U, T... >
{
Expand All @@ -798,7 +836,7 @@ template<class U, class... T> using resolve_overload_type = typename resolve_ove

#else

template<class U, class... T> using resolve_overload_type = typename decltype( overload<T...>()(std::declval<U>()) )::type;
template<class U, class... T> using resolve_overload_type = typename decltype( overload<T...>()(std::declval<U>(), std::declval<U>()) )::type;

#endif

Expand Down Expand Up @@ -1947,8 +1985,6 @@ template<class... T> constexpr bool operator>=( variant<T...> const & v, variant
namespace detail
{

template<class T> using remove_cv_ref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

template<class... T> variant<T...> const & extract_variant_base_( variant<T...> const & );

#if BOOST_WORKAROUND( BOOST_MSVC, < 1930 )
Expand Down
14 changes: 14 additions & 0 deletions test/variant_convert_construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ int main()
BOOST_TEST( holds_alternative<std::string>( v3 ) );
BOOST_TEST_EQ( get<std::string>( v2 ), get<std::string>( v3 ) );
}

{
variant<bool, std::string> v( "s2" );

variant<bool, bool, const char *, std::string> v2( v );

BOOST_TEST( holds_alternative<std::string>( v2 ) );
BOOST_TEST_EQ( get<std::string>( v ), get<std::string>( v2 ) );

variant<bool, bool, const char *, 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} };
Expand Down