Browse Source

type_traits: unpack_as_t/unpack_as_v

Michele Caini 5 years ago
parent
commit
f704352bae
2 changed files with 32 additions and 0 deletions
  1. 18 0
      src/entt/core/type_traits.hpp
  2. 14 0
      test/entt/core/type_traits.cpp

+ 18 - 0
src/entt/core/type_traits.hpp

@@ -12,6 +12,24 @@
 namespace entt {
 
 
+/**
+ * @brief Using declaration to be used to _repeat_ the same type a number of
+ * times equal to the size of a given parameter pack.
+ * @tparam Type A type to repeat.
+ */
+template<typename Type, typename>
+using unpack_as_t = Type;
+
+
+/**
+ * @brief Helper variable template to be used to _repeat_ the same value a
+ * number of times equal to the size of a given parameter pack.
+ * @tparam Value A value to repeat.
+ */
+template<auto Value, typename>
+inline constexpr auto unpack_as_v = Value;
+
+
 /**
  * @brief Wraps a static constant.
  * @tparam Value A static constant.

+ 14 - 0
test/entt/core/type_traits.cpp

@@ -4,6 +4,20 @@
 #include <entt/core/hashed_string.hpp>
 #include <entt/core/type_traits.hpp>
 
+TEST(Unpack, AsType) {
+    ASSERT_EQ([](auto &&... args) {
+        return [](entt::unpack_as_t<int, decltype(args)>... value) {
+            return (value + ... + 0);
+        };
+    }('c', 42., true)(1, 2, 3), 6);
+}
+
+TEST(Unpack, AsValue) {
+    ASSERT_EQ([](auto &&... args) {
+        return (entt::unpack_as_v<2, decltype(args)> + ... + 0);
+    }('c', 42., true), 6);
+}
+
 TEST(IntegralConstant, Functionalities) {
     entt::integral_constant<3> constant;