From 2085295358bf05b17c80a3b3418d6e5bf2940913 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 6 Jun 2025 12:17:49 +0100 Subject: [PATCH] #1119 Forced compiler error in nth_type.h breaks containers for C++03 --- include/etl/nth_type.h | 41 +++++++++++++++++++++++++++++++++++------ test/etl_profile.h | 1 + test/test_nth_type.cpp | 6 ++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/include/etl/nth_type.h b/include/etl/nth_type.h index 7617dc68..b1e62af5 100644 --- a/include/etl/nth_type.h +++ b/include/etl/nth_type.h @@ -32,13 +32,9 @@ SOFTWARE. #include "platform.h" #include "static_assert.h" -#if ETL_NOT_USING_CPP11 - #if !defined(ETL_IN_UNIT_TEST) - #error NOT SUPPORTED FOR C++03 OR BELOW - #endif -#else namespace etl { +#if ETL_USING_CPP11 && !defined(ETL_NTH_TYPE_FORCE_CPP03_IMPLEMENTATION) namespace private_nth_type { //*********************************** @@ -67,6 +63,39 @@ namespace etl //*********************************** template using nth_type_t = typename nth_type::type; +#else + // Primary template: Not defined, will cause a compile error if used with N > 7 or not enough types. + template + struct nth_type; + + // Specialisations for N = 0..7 + template + struct nth_type<0, T0, T1, T2, void, void, void, void, void> { typedef T0 type; }; + + template + struct nth_type<0, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T0 type; }; + + template + struct nth_type<1, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T1 type; }; + + template + struct nth_type<2, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T2 type; }; + + template + struct nth_type<3, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T3 type; }; + + template + struct nth_type<4, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T4 type; }; + + template + struct nth_type<5, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T5 type; }; + + template + struct nth_type<6, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T6 type; }; + + template + struct nth_type<7, T0, T1, T2, T3, T4, T5, T6, T7> { typedef T7 type; }; +#endif } #endif -#endif diff --git a/test/etl_profile.h b/test/etl_profile.h index e5cf5fd4..4edf41d1 100644 --- a/test/etl_profile.h +++ b/test/etl_profile.h @@ -103,6 +103,7 @@ SOFTWARE. #define ETL_FLAT_SET_FORCE_CPP03_IMPLEMENTATION #define ETL_FLAT_MULTISET_FORCE_CPP03_IMPLEMENTATION #define ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION + #define ETL_NTH_TYPE_FORCE_CPP03_IMPLEMENTATION #endif #if defined(ETL_FORCE_TEST_CPP11) diff --git a/test/test_nth_type.cpp b/test/test_nth_type.cpp index a5339292..192608c2 100644 --- a/test/test_nth_type.cpp +++ b/test/test_nth_type.cpp @@ -38,9 +38,15 @@ namespace //************************************************************************* TEST(test_nth_type) { +#if defined(ETL_NTH_TYPE_FORCE_CPP03_IMPLEMENTATION) + CHECK((std::is_same::type, int>::value)); + CHECK((std::is_same::type, long>::value)); + CHECK((std::is_same::type, double>::value)); +#else CHECK((std::is_same, int>::value)); CHECK((std::is_same, long>::value)); CHECK((std::is_same, double>::value)); +#endif } } }