diff --git a/include/etl/private/variant_variadic.h b/include/etl/private/variant_variadic.h index 8ec3f195..9571a2fd 100644 --- a/include/etl/private/variant_variadic.h +++ b/include/etl/private/variant_variadic.h @@ -253,7 +253,7 @@ namespace etl template struct variant_alternative> { - using type = etl::nth_type_t; + using type = etl::nth_type_t::type_list>; }; template @@ -370,7 +370,6 @@ namespace etl //*************************************************************************** /// A template class that can store any of the types defined in the template parameter list. - /// Supports up to 8 types. ///\ingroup variant //*************************************************************************** template @@ -378,6 +377,8 @@ namespace etl { public: + using type_list = etl::type_list; + //*************************************************************************** /// get() is a friend function. //*************************************************************************** @@ -1217,13 +1218,26 @@ namespace etl size_t type_id; }; + //*************************************************************************** + /// Delcare a variant using an etl::type_list. + //*************************************************************************** + template + class variant> : public etl::variant + { + public: + + using base_type = etl::variant; + using type_list = typename base_type::type_list; + using base_type::base_type; // inherit all ctors + }; + //*************************************************************************** /// Checks if the variant v holds the alternative T. //*************************************************************************** template ETL_CONSTEXPR14 bool holds_alternative(const etl::variant& v) ETL_NOEXCEPT - { - constexpr size_t Index = etl::type_list_index_of_type, T>::value; + { + constexpr size_t Index = etl::type_list_index_of_type::type_list, T>::value; return (Index == variant_npos) ? false : (v.index() == Index); } @@ -1254,7 +1268,7 @@ namespace etl get(etl::variant& v) { #if ETL_USING_CPP17 && !defined(ETL_VARIANT_FORCE_CPP11) - static_assert(Index < sizeof...(TTypes), "Index out of range"); + static_assert(Index < etl::type_list_size::type_list>::value, "Index out of range"); #endif ETL_ASSERT(Index == v.index(), ETL_ERROR(etl::variant_incorrect_type_exception)); @@ -1427,7 +1441,7 @@ namespace etl template struct variant_size> - : etl::integral_constant + : etl::integral_constant::type_list>::value> { }; diff --git a/test/test_variant_variadic.cpp b/test/test_variant_variadic.cpp index dc8373ac..6e91fa34 100644 --- a/test/test_variant_variadic.cpp +++ b/test/test_variant_variadic.cpp @@ -462,6 +462,45 @@ namespace test_variant_t variant_etl; + CHECK_TRUE((std::is_same>::value)); + + CHECK_TRUE(etl::holds_alternative(variant_etl)); + CHECK_FALSE(etl::holds_alternative(variant_etl)); + CHECK_FALSE(etl::holds_alternative(variant_etl)); + CHECK_EQUAL(1, etl::get<0U>(variant_etl).value); + + CHECK_TRUE(etl::holds_alternative<0U>(variant_etl)); + CHECK_FALSE(etl::holds_alternative<1U>(variant_etl)); + CHECK_FALSE(etl::holds_alternative<2U>(variant_etl)); + + CHECK_TRUE(etl::holds_alternative(0U, variant_etl)); + CHECK_FALSE(etl::holds_alternative(1U, variant_etl)); + CHECK_FALSE(etl::holds_alternative(2U, variant_etl)); + CHECK_FALSE(etl::holds_alternative(99U, variant_etl)); + } + + //************************************************************************* + TEST(test_constructor_default_variant_from_type_list) + { + struct DefaultConstructible + { + DefaultConstructible() + : value(1) + { + } + + int value = 0; + }; + + using variant_types = etl::type_list; + using test_variant_t = etl::variant; + + CHECK_NO_THROW(test_variant_t variant_etl); + + test_variant_t variant_etl; + + CHECK_TRUE((std::is_same::value)); + CHECK_TRUE(etl::holds_alternative(variant_etl)); CHECK_FALSE(etl::holds_alternative(variant_etl)); CHECK_FALSE(etl::holds_alternative(variant_etl));