Index: include/variant =================================================================== --- include/variant +++ include/variant @@ -1092,16 +1092,42 @@ struct __overload; template <> -struct __overload<> { void operator()() const; }; +struct __overload<> +{ + static void test(); +}; template -struct __overload<_Tp, _Types...> : __overload<_Types...> { - using __overload<_Types...>::operator(); - __identity<_Tp> operator()(_Tp) const; +struct __overload<_Tp, _Types...> : __overload<_Types...> +{ + using __overload<_Types...>::test; + + template + static auto test(_Tp, _Up&& __t) -> decltype(_Tp{__t}, __identity<_Tp>()); }; +#define _LIBCPP_VARIANT_BOOLEAN_CONVERSION(bool_type) \ + template \ + struct __overload : __overload<_Types...> \ + { \ + using __overload<_Types...>::test; \ + \ + template > \ + static auto test(bool, _Up&&) -> enable_if_t< \ + disjunction_v, is_class<_Ap>, is_union<_Ap>>, \ + __identity>; \ + } + +_LIBCPP_VARIANT_BOOLEAN_CONVERSION(bool); +_LIBCPP_VARIANT_BOOLEAN_CONVERSION(bool const); +_LIBCPP_VARIANT_BOOLEAN_CONVERSION(bool volatile); +_LIBCPP_VARIANT_BOOLEAN_CONVERSION(bool const volatile); + +#undef _LIBCPP_VARIANT_BOOLEAN_CONVERSION + template -using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type; +using __best_match_t = typename decltype( + __overload<_Types...>::test(declval<_Tp>(), declval<_Tp>()))::type; } // __variant_detail Index: test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp =================================================================== --- test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp +++ test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "test_macros.h" #include "variant_test_helpers.hpp" @@ -128,7 +129,7 @@ void test_T_assignment_sfinae() { { - using V = std::variant; + using V = std::variant; static_assert(!std::is_assignable::value, "ambiguous"); } { @@ -139,6 +140,15 @@ using V = std::variant; static_assert(!std::is_assignable::value, "no matching operator="); } + { + using V = std::variant; + static_assert(!std::is_assignable::value, "no matching operator="); + } + { + using V = std::variant, bool>; + static_assert(!std::is_assignable>::value, + "no explicit bool in operator="); + } #if !defined(TEST_VARIANT_HAS_NO_REFERENCES) { using V = std::variant; @@ -167,6 +177,33 @@ assert(v.index() == 1); assert(std::get<1>(v) == 43); } + { + std::variant v; + v = 42; + assert(v.index() == 1); + assert(std::get<1>(v) == 42); + v = 43u; + assert(v.index() == 0); + assert(std::get<0>(v) == 43); + } + { + std::variant v = true; + v = std::false_type(); + assert(v.index() == 1); + assert(std::get<1>(v) == false); + v = "bar"; + assert(v.index() == 0); + assert(std::get<0>(v) == "bar"); + } + { + std::variant> v; + v = nullptr; + assert(v.index() == 1); + assert(std::get<1>(v) == nullptr); + v = std::true_type(); + assert(v.index() == 0); + assert(std::get<0>(v)); + } #if !defined(TEST_VARIANT_HAS_NO_REFERENCES) { using V = std::variant; Index: test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp =================================================================== --- test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp +++ test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "test_convertible.hpp" #include "test_macros.h" @@ -53,7 +54,7 @@ void test_T_ctor_sfinae() { { - using V = std::variant; + using V = std::variant; static_assert(!std::is_constructible::value, "ambiguous"); } { @@ -66,6 +67,16 @@ "no matching constructor"); } { + using V = std::variant; + static_assert(!std::is_constructible::value, + "no matching constructor"); + } + { + using V = std::variant, bool>; + static_assert(!std::is_constructible>::value, + "no explicit bool in constructor"); + } + { using V = std::variant; static_assert( !std::is_constructible>::value, @@ -99,6 +110,21 @@ static_assert(v.index() == 1, ""); static_assert(std::get<1>(v) == 42, ""); } + { + constexpr std::variant v(42); + static_assert(v.index() == 1, ""); + static_assert(std::get<1>(v) == 42, ""); + } + { + std::variant v = "foo"; + assert(v.index() == 0); + assert(std::get<0>(v) == "foo"); + } + { + std::variant> v = nullptr; + assert(v.index() == 1); + assert(std::get<1>(v) == nullptr); + } #if !defined(TEST_VARIANT_HAS_NO_REFERENCES) { using V = std::variant;