diff --git a/include/etl/private/variant_variadic.h b/include/etl/private/variant_variadic.h index 4161aa31..31ac4ae8 100644 --- a/include/etl/private/variant_variadic.h +++ b/include/etl/private/variant_variadic.h @@ -261,6 +261,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. //******************************************* @@ -1023,22 +1092,27 @@ namespace etl /// Construct from a value. //*************************************************************************** #include "diagnostic_uninitialized_push.h" - template , variant>::value && !Trivial_, 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) { - static_assert(etl::is_one_of, TTypes...>::value, "Unsupported type"); - - construct_in_place >(data, etl::forward(value)); + construct_in_place(data, etl::forward(value)); } - template , variant>::value && Trivial_, 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) { - static_assert(etl::is_one_of, TTypes...>::value, "Unsupported type"); } #include "diagnostic_pop.h" @@ -1056,20 +1130,19 @@ namespace etl /// Construct from arguments. //*************************************************************************** #include "diagnostic_uninitialized_push.h" - template = 0> + template , TTypes...>::value, int> = 0> ETL_CONSTEXPR14 explicit variant(etl::in_place_type_t, TArgs&&... args) : base_type(index_of_type::value) { - static_assert(etl::is_one_of, TTypes...>::value, "Unsupported type"); - construct_in_place_args >(data, etl::forward(args)...); } - template = 0> + template , TTypes...>::value, int> = 0> constexpr explicit variant(etl::in_place_type_t, TArgs&&... args) : base_type(etl::in_place_index_t::value>{}, etl::remove_cvref_t(etl::forward(args)...), index_of_type::value) { - static_assert(etl::is_one_of, TTypes...>::value, "Unsupported type"); } #include "diagnostic_pop.h" @@ -1077,22 +1150,20 @@ namespace etl /// Construct from arguments. //*************************************************************************** #include "diagnostic_uninitialized_push.h" - template = 0> + template = 0> ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t, TArgs&&... args) : base_type(Index) { using type = type_from_index; - static_assert(etl::is_one_of::value, "Unsupported type"); - construct_in_place_args(data, etl::forward(args)...); } - template = 0> + template = 0> constexpr explicit variant(etl::in_place_index_t, TArgs&&... args) : base_type(etl::in_place_index_t{}, type_from_index(etl::forward(args)...), Index) { - using type = type_from_index; - static_assert(etl::is_one_of::value, "Unsupported type"); } #include "diagnostic_pop.h" @@ -1101,12 +1172,10 @@ namespace etl /// Construct from type, initializer_list and arguments. //*************************************************************************** #include "diagnostic_uninitialized_push.h" - template + template , TTypes...>::value, int> = 0> ETL_CONSTEXPR14 explicit variant(etl::in_place_type_t, std::initializer_list init, TArgs&&... args) : base_type(index_of_type::value) { - static_assert(etl::is_one_of, TTypes...>::value, "Unsupported type"); - construct_in_place_args >(data, init, etl::forward(args)...); } #include "diagnostic_pop.h" @@ -1115,13 +1184,11 @@ namespace etl /// Construct from index, initializer_list and arguments. //*************************************************************************** #include "diagnostic_uninitialized_push.h" - template + template = 0> ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t, std::initializer_list init, TArgs&&... args) : base_type(Index) { using type = type_from_index; - static_assert(etl::is_one_of::value, "Unsupported type"); - construct_in_place_args(data, init, etl::forward(args)...); } #include "diagnostic_pop.h" @@ -1149,11 +1216,9 @@ namespace etl //*************************************************************************** /// Emplace by type with variadic constructor parameters. //*************************************************************************** - template + template ::value, int> = 0> T& emplace(TArgs&&... args) ETL_NOEXCEPT_IF((etl::is_nothrow_constructible, TArgs...>::value)) { - static_assert(etl::is_one_of::value, "Unsupported type"); - using type = etl::remove_cvref_t; do_destroy(); @@ -1168,12 +1233,10 @@ namespace etl //*************************************************************************** /// Emplace by type with variadic constructor parameters. //*************************************************************************** - template + template ::value, int> = 0> T& emplace(std::initializer_list il, TArgs&&... args) ETL_NOEXCEPT_IF((etl::is_nothrow_constructible, std::initializer_list, TArgs...>::value)) { - static_assert(etl::is_one_of::value, "Unsupported type"); - using type = etl::remove_cvref_t; do_destroy(); @@ -1188,12 +1251,10 @@ namespace etl //*************************************************************************** /// Emplace by index with variadic constructor parameters. //*************************************************************************** - template + template = 0> typename etl::variant_alternative_t >& emplace(TArgs&&... args) ETL_NOEXCEPT_IF((etl::is_nothrow_constructible, TArgs...>::value)) { - static_assert(Index < sizeof...(TTypes), "Index out of range"); - using type = type_from_index; do_destroy(); @@ -1208,12 +1269,10 @@ namespace etl //*************************************************************************** /// Emplace by index with variadic constructor parameters. //*************************************************************************** - template + template = 0> typename etl::variant_alternative_t >& emplace(std::initializer_list il, TArgs&&... args) ETL_NOEXCEPT_IF((etl::is_nothrow_constructible, std::initializer_list, TArgs...>::value)) { - static_assert(Index < sizeof...(TTypes), "Index out of range"); - using type = type_from_index; do_destroy(); @@ -1229,17 +1288,15 @@ namespace etl /// Move assignment operator for type. ///\param value The value to assign. //*************************************************************************** - template , variant>::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; - - static_assert(etl::is_one_of::value, "Unsupported type"); - 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 8e81a606..bfa8b953 100644 --- a/test/test_variant_variadic.cpp +++ b/test/test_variant_variadic.cpp @@ -807,7 +807,7 @@ namespace } //************************************************************************* - TEST(test_constructor_value) + TEST(test_constructor_value_no_conversion) { // Char. char c = 'a'; @@ -831,6 +831,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) { @@ -1379,12 +1474,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)); } //************************************************************************* @@ -2932,6 +3034,21 @@ namespace CHECK(etl::get_if(&v1) != nullptr); } + //************************************************************************* + TEST(test_variant_implicit_conversion_constraints) + { + struct overloaded + { + overloaded(etl::variant&&) {} + overloaded(etl::variant&&) {} + }; + + /* Construct from value, trivially destructible suite */ + overloaded(1); + /* Construct from value, non-trivially destructible suite */ + overloaded(1.0f); + } + //************************************************************************* // Tests for noexcept properties of etl::variant // The noexcept specs only take effect when ETL_USING_EXCEPTIONS is enabled,