Skip to content

Commit

Permalink
etl/type_traits.h: Add support for type_identity (#905)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tigran2008 authored Jun 11, 2024
1 parent bb71b60 commit 6ced063
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/etl/generators/type_traits_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,17 @@ typedef integral_constant<bool, true> true_type;
template <typename T>
using signed_type_t = typename signed_type<T>::type;
#endif

//*********************************************
// type_identity

template <typename T>
struct type_identity { typedef T type; };

#if ETL_USING_CPP11
template <typename T>
using type_identity_t = typename type_identity<T>::type;
#endif
}

// Helper macros
Expand Down
11 changes: 11 additions & 0 deletions include/etl/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,17 @@ typedef integral_constant<bool, true> true_type;
template <typename T>
using signed_type_t = typename signed_type<T>::type;
#endif

//*********************************************
// type_identity

template <typename T>
struct type_identity { typedef T type; };

#if ETL_USING_CPP11
template <typename T>
using type_identity_t = typename type_identity<T>::type;
#endif
}

// Helper macros
Expand Down
12 changes: 12 additions & 0 deletions test/test_type_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ namespace
NotDefaultConstructible(NotDefaultConstructible&&) = delete;
NotDefaultConstructible& operator =(NotDefaultConstructible&) = delete;
};

// A function to test etl::type_identity.
template <typename T>
T type_identity_test_add(T first, typename etl::type_identity<T>::type second)
{
return first + second;
}
}

// Definitions for when the STL and compiler built-ins are not available.
Expand Down Expand Up @@ -1320,4 +1327,9 @@ namespace
CHECK_FALSE(bool(etl::is_base_of_all<Base, D1, D2, D3, D4>::value));
#endif
}

//*************************************************************************
TEST(test_type_identity) {
CHECK_CLOSE(type_identity_test_add(1.5f, 2), 3.5f, 0.01f);
}
}

0 comments on commit 6ced063

Please sign in to comment.