Quellcode durchsuchen

type_traits: added constness_as[_t]

Michele Caini vor 5 Jahren
Ursprung
Commit
58dbaf4a51
2 geänderte Dateien mit 35 neuen und 1 gelöschten Zeilen
  1. 28 1
      src/entt/core/type_traits.hpp
  2. 7 0
      test/entt/core/type_traits.cpp

+ 28 - 1
src/entt/core/type_traits.hpp

@@ -63,7 +63,7 @@ using integral_constant = std::integral_constant<decltype(Value), Value>;
 
 
 /**
- * @brief Alias template to ease the creation of named values.
+ * @brief Alias template to facilitate the creation of named values.
  * @tparam Value A constant value at least convertible to `id_type`.
  */
 template<id_type Value>
@@ -322,6 +322,33 @@ template<typename Type>
 inline constexpr auto is_empty_v = is_empty<Type>::value;
 
 
+/**
+ * @brief Transcribes the constness of a type to another type.
+ * @tparam To The type to which to transcribe the constness.
+ * @tparam From The type from which to transcribe the constness.
+ */
+template<typename To, typename From>
+struct constness_as {
+    using type = std::remove_const_t<To>;
+};
+
+
+/*! @copydoc constness_as */
+template<typename To, typename From>
+struct constness_as<To, const From> {
+    using type = std::add_const_t<To>;
+};
+
+
+/**
+ * @brief Alias template to facilitate the transcription of the constness.
+ * @tparam To The type to which to transcribe the constness.
+ * @tparam From The type from which to transcribe the constness.
+ */
+template<typename To, typename From>
+using constness_as_t = typename constness_as<To, From>::type;
+
+
 /**
  * @brief Extracts the class of a non-static member object or function.
  * @tparam Member A pointer to a non-static member object or function.

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

@@ -70,6 +70,13 @@ TEST(TypeTraits, IsApplicable) {
     static_assert(!entt::is_applicable_r_v<int, int(int, char), std::tuple<void>>);
 }
 
+TEST(TypeTraits, ConstnessAs) {
+    static_assert(std::is_same_v<entt::constness_as_t<int, char>, int>);
+    static_assert(std::is_same_v<entt::constness_as_t<const int, char>, int>);
+    static_assert(std::is_same_v<entt::constness_as_t<int, const char>, const int>);
+    static_assert(std::is_same_v<entt::constness_as_t<const int, const char>, const int>);
+}
+
 TEST(TypeTraits, MemberClass) {
     struct clazz {
         char foo(int) { return {}; }