From dffd86544ce2e8e76350fed7a93aacb0fd97aaa8 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 12 Jul 2025 12:52:42 +0100 Subject: [PATCH] Added etl::type_lists_are_convertible --- include/etl/type_list.h | 29 +++++++++++++++++++++++++++++ test/test_type_list.cpp | 29 ++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/include/etl/type_list.h b/include/etl/type_list.h index 4dbc66ec..2042ff79 100644 --- a/include/etl/type_list.h +++ b/include/etl/type_list.h @@ -287,6 +287,35 @@ namespace etl template using type_list_cat_t = typename type_list_cat::type; + + //*************************************************************************** + /// Checks that two type lists are convertible. + /// Static asserts if the type lists are not the same length. + //*************************************************************************** + // Primary template + template + struct type_lists_are_convertible; + + // Specialization: both lists empty, convertible + template <> + struct type_lists_are_convertible, etl::type_list<>> + : public etl::true_type + { + }; + + // Recursive case: check head types, then recurse + template + struct type_lists_are_convertible, etl::type_list> + : public etl::bool_constant::value && + etl::type_lists_are_convertible, etl::type_list>::value> + { + static_assert(sizeof...(TFromTail) == sizeof...(TToTail), "Type lists are not the same length"); + }; + +#if ETL_USING_CPP17 + template + inline constexpr bool type_lists_are_convertible_v = etl::type_lists_are_convertible::value; +#endif } #endif diff --git a/test/test_type_list.cpp b/test/test_type_list.cpp index de9be9fc..5cd253d0 100644 --- a/test/test_type_list.cpp +++ b/test/test_type_list.cpp @@ -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 t1; + typedef etl::type_list t2; + typedef etl::type_list t3; + typedef etl::type_list<> t4; + + // Uncomment to generate static_assert error. + //typedef etl::type_list t5; + + CHECK_TRUE((etl::type_lists_are_convertible::value)); + CHECK_FALSE((etl::type_lists_are_convertible::value)); + CHECK_TRUE((etl::type_lists_are_convertible::value)); + + // Uncomment to generate static_assert error. + //CHECK_FALSE((etl::type_lists_are_convertible::value)); + +#if ETL_USING_CPP17 + CHECK_TRUE((etl::type_lists_are_convertible_v)); + CHECK_FALSE((etl::type_lists_are_convertible_v)); + CHECK_TRUE((etl::type_lists_are_convertible_v)); + + // Uncomment to generate static_assert error. + //CHECK_FALSE((etl::type_lists_are_convertible_v)); +#endif + } }; #endif }