Added etl::type_lists_are_convertible

This commit is contained in:
John Wellbelove 2025-07-12 12:52:42 +01:00
parent 400f438958
commit dffd86544c
2 changed files with 57 additions and 1 deletions

View File

@ -287,6 +287,35 @@ namespace etl
template <typename... TypeLists>
using type_list_cat_t = typename type_list_cat<TypeLists...>::type;
//***************************************************************************
/// Checks that two type lists are convertible.
/// Static asserts if the type lists are not the same length.
//***************************************************************************
// Primary template
template <typename TFromList, typename TToList>
struct type_lists_are_convertible;
// Specialization: both lists empty, convertible
template <>
struct type_lists_are_convertible<etl::type_list<>, etl::type_list<>>
: public etl::true_type
{
};
// Recursive case: check head types, then recurse
template <typename TFromHead, typename... TFromTail, typename TToHead, typename... TToTail>
struct type_lists_are_convertible<etl::type_list<TFromHead, TFromTail...>, etl::type_list<TToHead, TToTail...>>
: public etl::bool_constant<etl::is_convertible<TFromHead, TToHead>::value &&
etl::type_lists_are_convertible<etl::type_list<TFromTail...>, etl::type_list<TToTail...>>::value>
{
static_assert(sizeof...(TFromTail) == sizeof...(TToTail), "Type lists are not the same length");
};
#if ETL_USING_CPP17
template <typename TFromList, typename TToList>
inline constexpr bool type_lists_are_convertible_v = etl::type_lists_are_convertible<TFromList, TToList>::value;
#endif
}
#endif

View File

@ -5,7 +5,7 @@ Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2025 BMW AG
Copyright(c) 2025 BMW AG, John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
@ -180,6 +180,33 @@ namespace
#endif
}
//*************************************************************************
TEST(test_type_lists_are_convertible)
{
typedef etl::type_list<char, int, uint32_t> t1;
typedef etl::type_list<short, long, uint64_t> t2;
typedef etl::type_list<char, std::string, uint32_t> t3;
typedef etl::type_list<> t4;
// Uncomment to generate static_assert error.
//typedef etl::type_list<char, int> t5;
CHECK_TRUE((etl::type_lists_are_convertible<t1, t2>::value));
CHECK_FALSE((etl::type_lists_are_convertible<t1, t3>::value));
CHECK_TRUE((etl::type_lists_are_convertible<t4, t4>::value));
// Uncomment to generate static_assert error.
//CHECK_FALSE((etl::type_lists_are_convertible<t1, t5>::value));
#if ETL_USING_CPP17
CHECK_TRUE((etl::type_lists_are_convertible_v<t1, t2>));
CHECK_FALSE((etl::type_lists_are_convertible_v<t1, t3>));
CHECK_TRUE((etl::type_lists_are_convertible_v<t4, t4>));
// Uncomment to generate static_assert error.
//CHECK_FALSE((etl::type_lists_are_convertible_v<t1, t5>));
#endif
}
};
#endif
}