From 63d74706850f056d63d5b19465c4cee8d6eab3a7 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Mon, 18 Aug 2025 14:22:33 +0100 Subject: [PATCH] Added etl::nontype_t, with C++11 and C++17 alternatives --- include/etl/utility.h | 20 ++++++++++++++++++++ test/test_utility.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/include/etl/utility.h b/include/etl/utility.h index 43852e72..c3ea993d 100644 --- a/include/etl/utility.h +++ b/include/etl/utility.h @@ -818,6 +818,26 @@ namespace etl TReturn(*ptr)(TArgs...); }; #endif + +#if ETL_USING_CPP17 && !defined(ETL_FORCE_CPP11_NONTYPE) + //***************************************************************************** + // Wraps a non-type template parameter as a type. + //***************************************************************************** + template + struct nontype_t + { + static constexpr decltype(Value) value = Value; + }; +#elif ETL_USING_CPP11 + //***************************************************************************** + // Wraps a non-type template parameter as a type. + //***************************************************************************** + template + struct nontype_t + { + static constexpr T value = Value; + }; +#endif } #endif diff --git a/test/test_utility.cpp b/test/test_utility.cpp index 4b378625..0a9559e6 100644 --- a/test/test_utility.cpp +++ b/test/test_utility.cpp @@ -789,4 +789,42 @@ namespace } #endif }; + +#if ETL_USING_CPP17 && !defined(ETL_FORCE_CPP11_NONTYPE) + //********************************* + TEST(test_nontype_t_cpp17) + { + // Test with int + CHECK_TRUE(42 == etl::nontype_t<42>::value); + + // Test with bool + CHECK_TRUE(true == etl::nontype_t::value); + + // Test with char + CHECK_TRUE('A' == etl::nontype_t<'A'>::value); + + // Test with enum + enum class E : uint8_t { A = 1, B = 2 }; + CHECK_TRUE(E::A == etl::nontype_t::value); + CHECK_TRUE(E::B == etl::nontype_t::value); + } +#elif ETL_USING_CPP11 + enum class E : uint8_t { A = 1, B = 2 }; + + TEST(test_nontype_t_cpp11) + { + // Test with int + CHECK_TRUE(42 == (etl::nontype_t::value)); + + // Test with bool + CHECK_TRUE(true == (etl::nontype_t::value)); + + // Test with char + CHECK_TRUE('A' == (etl::nontype_t::value)); + + // Test with enum + CHECK_TRUE(E::A == (etl::nontype_t::value)); + CHECK_TRUE(E::B == (etl::nontype_t::value)); + } +#endif }