Added etl::nontype_t, with C++11 and C++17 alternatives

This commit is contained in:
John Wellbelove 2025-08-18 14:22:33 +01:00
parent 450948933f
commit 63d7470685
2 changed files with 58 additions and 0 deletions

View File

@ -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 <auto Value>
struct nontype_t
{
static constexpr decltype(Value) value = Value;
};
#elif ETL_USING_CPP11
//*****************************************************************************
// Wraps a non-type template parameter as a type.
//*****************************************************************************
template <typename T, T Value>
struct nontype_t
{
static constexpr T value = Value;
};
#endif
}
#endif

View File

@ -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<true>::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<E::A>::value);
CHECK_TRUE(E::B == etl::nontype_t<E::B>::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<int, 42>::value));
// Test with bool
CHECK_TRUE(true == (etl::nontype_t<bool, true>::value));
// Test with char
CHECK_TRUE('A' == (etl::nontype_t<char, 'A'>::value));
// Test with enum
CHECK_TRUE(E::A == (etl::nontype_t<E, E::A>::value));
CHECK_TRUE(E::B == (etl::nontype_t<E, E::B>::value));
}
#endif
}