diff --git a/include/etl/private/variant_variadic.h b/include/etl/private/variant_variadic.h index f45be48b..73cf5cbc 100644 --- a/include/etl/private/variant_variadic.h +++ b/include/etl/private/variant_variadic.h @@ -283,6 +283,75 @@ namespace etl } }; + //**************************************************************************************************** + /// Helpers for selecting the best conversion candidate for variant value construction. + /// Only implicit conversions that do not involve narrowing are considered. + //**************************************************************************************************** + // Used to check for valid conversion that don't include narrowing as array copy-list-initialization excludes narrowing conversions. + template + struct conversion_array + { + T value[1]; + }; + + // Create a candidate function accepting TAlternative provided it can be constructed from T without narrowing conversions. When the conversion + // is valid, the function returns an integral constant representing the index of the alternative type in the variant's type list. + template + struct conversion_candidate + { + static void select(); + }; + + template + struct conversion_candidate{{etl::declval()}})> > + { + static etl::integral_constant select(TAlternative); + }; + + // Build all conversion overloads for a given type T across the list of alternative types. + template + struct conversion_overloads_impl; + + template + struct conversion_overloads_impl + { + static void select(); + }; + + template + struct conversion_overloads_impl + : conversion_candidate + , conversion_overloads_impl + { + using conversion_candidate::select; + using conversion_overloads_impl::select; + }; + + template + using conversion_overloads = conversion_overloads_impl; + + // Select the best alternative type for a given type T. + template + using select_alternative_index = decltype(conversion_overloads::select(etl::declval())); + + //************************************************************************* + /// Helpers for identifying in-place tags. + //************************************************************************* + template + struct is_in_place_tag : etl::false_type + { + }; + + template + struct is_in_place_tag > : etl::true_type + { + }; + + template + struct is_in_place_tag > : etl::true_type + { + }; + //******************************************* /// Constexpr get by index from variadic_union. //******************************************* @@ -1016,22 +1085,26 @@ namespace etl /// Construct from a value. //*************************************************************************** #include "diagnostic_uninitialized_push.h" - template < - typename T, bool Trivial_ = Is_Trivially_Destructible_Suite, - etl::enable_if_t, variant>::value && !Trivial_ && etl::is_one_of, TTypes...>::value, - int> = 0> + template < typename T, bool Trivial_ = Is_Trivially_Destructible_Suite, + etl::enable_if_t< !etl::is_same, variant>::value && !Trivial_ + && !private_variant::is_in_place_tag >::value, + int> = 0, + size_t AlternativeIndex = private_variant::select_alternative_index::value, + typename TAlternative = type_from_index, etl::enable_if_t::value, int> = 0> ETL_CONSTEXPR14 variant(T&& value) - : base_type(index_of_type::value) + : base_type(AlternativeIndex) { - construct_in_place >(data, etl::forward(value)); + construct_in_place(data, etl::forward(value)); } - template < - typename T, bool Trivial_ = Is_Trivially_Destructible_Suite, - etl::enable_if_t, variant>::value && Trivial_ && etl::is_one_of, TTypes...>::value, - int> = 0> + template < typename T, bool Trivial_ = Is_Trivially_Destructible_Suite, + etl::enable_if_t, variant>::value && Trivial_ + && !private_variant::is_in_place_tag >::value, + int> = 0, + size_t AlternativeIndex = private_variant::select_alternative_index::value, + typename TAlternative = type_from_index, etl::enable_if_t::value, int> = 0 > constexpr variant(T&& value) - : base_type(etl::in_place_index_t::value>{}, etl::forward(value), index_of_type::value) + : base_type(etl::in_place_index_t{}, etl::forward(value), AlternativeIndex) { } #include "diagnostic_pop.h" @@ -1208,17 +1281,15 @@ namespace etl /// Move assignment operator for type. ///\param value The value to assign. //*************************************************************************** - template < - typename T, - etl::enable_if_t, variant>::value && etl::is_one_of, TTypes...>::value, int> = 0> + template < typename T, etl::enable_if_t, variant>::value, int> = 0, + size_t AlternativeIndex = private_variant::select_alternative_index::value, + typename TAlternative = type_from_index, etl::enable_if_t::value, int> = 0> variant& operator=(T&& value) { - using type = etl::remove_cvref_t; - do_destroy(); - do_construct(etl::forward(value)); + do_construct(etl::forward(value)); - type_id = index_of_type::value; + type_id = AlternativeIndex; return *this; } diff --git a/test/test_variant_variadic.cpp b/test/test_variant_variadic.cpp index de09ff68..b7e69adb 100644 --- a/test/test_variant_variadic.cpp +++ b/test/test_variant_variadic.cpp @@ -831,7 +831,7 @@ namespace } //************************************************************************* - TEST(test_constructor_value) + TEST(test_constructor_value_no_conversion) { // Char. char c = 'a'; @@ -855,6 +855,101 @@ namespace CHECK_EQUAL(text, etl::get(variant_text_etl)); } + //************************************************************************* + TEST(test_constructor_value_conversion) + { + // Non-narrowing numeric conversions should be allowed. + etl::variant variant_double_trivial{1.5f}; + CHECK_EQUAL(etl::holds_alternative(variant_double_trivial), true); + CHECK_EQUAL(1.5, etl::get(variant_double_trivial)); + + etl::variant variant_double_nontrivial{1.5f}; + CHECK_EQUAL(etl::holds_alternative(variant_double_nontrivial), true); + CHECK_EQUAL(1.5, etl::get(variant_double_nontrivial)); + + // Implicit conversions for non-numeric types should also be allowed. + struct ImplicitInt + { + operator int() const + { + return 42; + } + }; + + etl::variant variant_implicit_int_trivial{ImplicitInt{}}; + CHECK_EQUAL(etl::holds_alternative(variant_implicit_int_trivial), true); + CHECK_EQUAL(42, etl::get(variant_implicit_int_trivial)); + + etl::variant variant_implicit_int_nontrivial{ImplicitInt{}}; + CHECK_EQUAL(etl::holds_alternative(variant_implicit_int_nontrivial), true); + CHECK_EQUAL(42, etl::get(variant_implicit_int_nontrivial)); + + // Exact match should be chosen over other conversions. + etl::variant variant_float_trivial{1.5f}; + CHECK_EQUAL(etl::holds_alternative(variant_float_trivial), true); + CHECK_EQUAL(1.5f, etl::get(variant_float_trivial)); + + etl::variant variant_float_nontrivial{1.5f}; + CHECK_EQUAL(etl::holds_alternative(variant_float_nontrivial), true); + CHECK_EQUAL(1.5f, etl::get(variant_float_nontrivial)); + + // Conversion should be possible even in the presence of duplicate alternatives. + etl::variant variant_duplicate_trivial{1.5f}; + CHECK_EQUAL(etl::holds_alternative(variant_duplicate_trivial), true); + CHECK_EQUAL(1.5, etl::get(variant_duplicate_trivial)); + + etl::variant variant_duplicate_nontrivial{1.5f}; + CHECK_EQUAL(etl::holds_alternative(variant_duplicate_nontrivial), true); + CHECK_EQUAL(1.5, etl::get(variant_duplicate_nontrivial)); + + // Narrowing conversion should not be allowed. + CHECK(!(etl::is_constructible, double>::value)); + CHECK(!(etl::is_constructible, double>::value)); + CHECK(!(std::is_constructible, float>::value)); + CHECK(!(std::is_constructible, float>::value)); + CHECK(!(std::is_constructible, int>::value)); + CHECK(!(std::is_constructible, int>::value)); + CHECK(!(std::is_constructible, int>::value)); + CHECK(!(std::is_constructible, int>::value)); + + // Construction should not be allowed when no implicit conversion is possible. + struct ExplicitInt + { + explicit operator int() const + { + return 42; + } + }; + + CHECK(!(std::is_constructible, const int*>::value)); + CHECK(!(std::is_constructible, const int*>::value)); + CHECK(!(std::is_constructible, ExplicitInt>::value)); + CHECK(!(std::is_constructible, ExplicitInt>::value)); + + // Construction should not be allowed when the conversion is ambiguous. + struct AmbiguousIntLong + { + operator int() const + { + return 1; + } + + operator long() const + { + return 1L; + } + }; + + CHECK(!(std::is_constructible, AmbiguousIntLong>::value)); + CHECK(!(std::is_constructible, AmbiguousIntLong>::value)); + + // The converting constructor should not accept in-place type or index tags. + CHECK((!std::is_constructible>, etl::in_place_type_t>::value)); + CHECK((!std::is_constructible>, etl::in_place_type_t>::value)); + CHECK((!std::is_constructible>, etl::in_place_index_t<1U>>::value)); + CHECK((!std::is_constructible>, etl::in_place_index_t<2U>>::value)); + } + //************************************************************************* TEST(test_constructor_move_value) { @@ -1535,12 +1630,19 @@ namespace //************************************************************************* TEST(test_assign_from_value) { + // No conversion required std::string text("Some Text"); test_variant_etl_3 variant_etl; variant_etl = text; CHECK_EQUAL(text, etl::get(variant_etl)); + + // Conversion from float to double required + etl::variant variant_double; + variant_double = 1.5f; + + CHECK_EQUAL(1.5, etl::get(variant_double)); } //*************************************************************************