From 6ced0630a948f5b3afe62ae82e135b7500ac8100 Mon Sep 17 00:00:00 2001 From: Tigran Khachatryan <39616689+tigran2008@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:43:59 +0400 Subject: [PATCH] etl/type_traits.h: Add support for type_identity (#905) * etl/type_traits.h: Add support for type_identity * test/test_type_traits.cpp: Add a test for etl::type_identity (type_identity_test_add(1.5f, 2) == 3.5f) * Update test_type_traits.cpp Use CHECK_CLOSE instead of CHECK for equality --- include/etl/generators/type_traits_generator.h | 11 +++++++++++ include/etl/type_traits.h | 11 +++++++++++ test/test_type_traits.cpp | 12 ++++++++++++ 3 files changed, 34 insertions(+) diff --git a/include/etl/generators/type_traits_generator.h b/include/etl/generators/type_traits_generator.h index 35db7ea6..c1d73d10 100644 --- a/include/etl/generators/type_traits_generator.h +++ b/include/etl/generators/type_traits_generator.h @@ -2295,6 +2295,17 @@ typedef integral_constant true_type; template using signed_type_t = typename signed_type::type; #endif + +//********************************************* +// type_identity + +template +struct type_identity { typedef T type; }; + +#if ETL_USING_CPP11 + template + using type_identity_t = typename type_identity::type; +#endif } // Helper macros diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index 93730e5a..1a40f026 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -2288,6 +2288,17 @@ typedef integral_constant true_type; template using signed_type_t = typename signed_type::type; #endif + +//********************************************* +// type_identity + +template +struct type_identity { typedef T type; }; + +#if ETL_USING_CPP11 + template + using type_identity_t = typename type_identity::type; +#endif } // Helper macros diff --git a/test/test_type_traits.cpp b/test/test_type_traits.cpp index 129a5fa8..fc9e7070 100644 --- a/test/test_type_traits.cpp +++ b/test/test_type_traits.cpp @@ -122,6 +122,13 @@ namespace NotDefaultConstructible(NotDefaultConstructible&&) = delete; NotDefaultConstructible& operator =(NotDefaultConstructible&) = delete; }; + + // A function to test etl::type_identity. + template + T type_identity_test_add(T first, typename etl::type_identity::type second) + { + return first + second; + } } // Definitions for when the STL and compiler built-ins are not available. @@ -1320,4 +1327,9 @@ namespace CHECK_FALSE(bool(etl::is_base_of_all::value)); #endif } + + //************************************************************************* + TEST(test_type_identity) { + CHECK_CLOSE(type_identity_test_add(1.5f, 2), 3.5f, 0.01f); + } }