Browse Source

type_info: a more reliable operator bool

Michele Caini 5 years ago
parent
commit
4162d4fbc6
2 changed files with 13 additions and 4 deletions
  1. 2 2
      src/entt/core/type_info.hpp
  2. 11 2
      test/entt/core/type_info.cpp

+ 2 - 2
src/entt/core/type_info.hpp

@@ -38,7 +38,7 @@ template<typename Type>
     auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
     return value;
 #else
-    return std::string_view{};
+    return std::string_view{""};
 #endif
 }
 
@@ -185,7 +185,7 @@ public:
      * @return True if the object is properly initialized, false otherwise.
      */
     [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
-        return !name_value.empty();
+        return name_value.data() != nullptr;
     }
 
     /**

+ 11 - 2
test/entt/core/type_info.cpp

@@ -5,6 +5,13 @@
 #include <entt/core/type_info.hpp>
 #include <entt/core/type_traits.hpp>
 
+template<>
+struct entt::type_name<float> final {
+    [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
+        return std::string_view{""};
+    }
+};
+
 TEST(TypeSeq, Functionalities) {
     ASSERT_EQ(entt::type_seq<int>::value(), entt::type_seq<int>::value());
     ASSERT_NE(entt::type_seq<int>::value(), entt::type_seq<char>::value());
@@ -44,7 +51,8 @@ TEST(TypeInfo, Functionalities) {
     ASSERT_NE(entt::type_id<int>(), entt::type_info{});
     ASSERT_NE(entt::type_id<int>(), entt::type_id<char>());
 
-    auto info = entt::type_id<int>();
+    const auto info = entt::type_id<int>();
+    const auto unnamed = entt::type_id<float>();
     entt::type_info empty{};
 
     ASSERT_NE(info, empty);
@@ -55,8 +63,9 @@ TEST(TypeInfo, Functionalities) {
     ASSERT_EQ(info.hash(), entt::type_hash<int>::value());
     ASSERT_EQ(info.name(), entt::type_name<int>::value());
 
-    ASSERT_FALSE(empty);
     ASSERT_TRUE(info);
+    ASSERT_TRUE(unnamed);
+    ASSERT_FALSE(empty);
 
     empty = info;