Allow etl::make_index_sequence to be created from an etl::type_list

This commit is contained in:
John Wellbelove 2026-01-28 17:33:32 +00:00
parent a4e4527669
commit a61980660f
2 changed files with 58 additions and 1 deletions

View File

@ -565,8 +565,31 @@ namespace etl
template <size_t Count>
using make_index_sequence = typename private_integer_sequence::make_index_sequence<Count, etl::integer_sequence<size_t>>::type;
//***********************************
// Helper to support both parameter packs and etl::type_list<T...>
// Forward declare etl::type_list to allow use without including type_list.h
template <typename... TTypes>
using make_index_sequence_for = typename private_integer_sequence::make_index_sequence<sizeof...(TTypes), etl::integer_sequence<size_t>>::type;
struct type_list;
namespace private_make_index_sequence_for
{
// Generic pack form
template <typename... TTypes>
struct impl
{
using type = typename private_integer_sequence::make_index_sequence<sizeof...(TTypes), etl::integer_sequence<size_t>>::type;
};
// etl::type_list form
template <typename... TTypes>
struct impl<etl::type_list<TTypes...>> : impl<TTypes...>
{
};
}
// Accepts either a parameter pack of types or a single etl::type_list<T...>
template <typename... TTypes>
using make_index_sequence_for = typename private_make_index_sequence_for::impl<TTypes...>::type;
//***********************************
template <size_t... Indices>

View File

@ -863,5 +863,39 @@ namespace
CHECK_TRUE(E::B == (etl::nontype_t<E, E::B>::value));
}
#endif
//*********************************
TEST(test_make_index_sequence_for_pack_matches_expected)
{
struct T1 {};
struct T2 {};
struct T3 {};
using seq12 = etl::make_index_sequence_for<T1, T2>;
using seq123 = etl::make_index_sequence_for<T1, T2, T3>;
using expect12 = etl::index_sequence<0U, 1U>;
using expect123 = etl::index_sequence<0U, 1U, 2U>;
CHECK_TRUE((std::is_same<seq12, expect12>::value));
CHECK_TRUE((std::is_same<seq123, expect123>::value));
}
//*********************************
TEST(test_make_index_sequence_for_type_list_matches_expected)
{
struct T1 {};
struct T2 {};
struct T3 {};
using list12 = etl::type_list<T1, T2>;
using list123 = etl::type_list<T1, T2, T3>;
using seq12 = etl::make_index_sequence_for<list12>;
using seq123 = etl::make_index_sequence_for<list123>;
using expect12 = etl::index_sequence<0U, 1U>;
using expect123 = etl::index_sequence<0U, 1U, 2U>;
CHECK_TRUE((std::is_same<seq12, expect12>::value));
CHECK_TRUE((std::is_same<seq123, expect123>::value));
}
}
}