type_info: a default constructible type isn't a good idea actually

This commit is contained in:
Michele Caini
2022-04-08 12:17:14 +02:00
parent 4e13529adb
commit dcac7942fa
3 changed files with 13 additions and 36 deletions

View File

@@ -559,9 +559,8 @@ require to enable RTTI.<br/>
Therefore, they can sometimes be even more reliable than those obtained
otherwise.
Its type defines a default constructible opaque class that is also copyable and
movable.<br/>
Objects of this class are generally returned by the `type_id` functions:
Its type defines an opaque class that is also copyable and movable.<br/>
Objects of this type are generally returned by the `type_id` functions:
```cpp
// by type
@@ -571,11 +570,16 @@ auto info = entt::type_id<a_type>();
auto other = entt::type_id(42);
```
The objects thus received are nothing more than const references to instances of
`type_info` with static storage duration.<br/>
All elements thus received are nothing more than const references to instances
of `type_info` with static storage duration.<br/>
This is convenient for saving the entire object aside for the cost of a pointer.
However, nothing prevents from constructing `type_info` objects directly.<br/>
These are the information made available by this kind of objects:
However, nothing prevents from constructing `type_info` objects directly:
```cpp
entt::type_info info{std::in_place_type<int>};
```
These are the information made available by `type_info`:
* The index associated with a given type:

View File

@@ -21,8 +21,7 @@ namespace internal {
struct ENTT_API type_index final {
[[nodiscard]] static id_type next() ENTT_NOEXCEPT {
static ENTT_MAYBE_ATOMIC(id_type) value{};
// 0u is reserved for empty type_info objects
return ++value;
return value++;
}
};
@@ -140,12 +139,6 @@ struct type_name final {
/*! @brief Implementation specific information about a type. */
struct type_info final {
/*! @brief Default constructor. */
constexpr type_info() ENTT_NOEXCEPT
: seq{},
identifier{},
alias{} {}
/**
* @brief Constructs a type info object for a given type.
* @tparam Type Type for which to construct a type info object.
@@ -154,9 +147,7 @@ struct type_info final {
constexpr type_info(std::in_place_type_t<Type>) ENTT_NOEXCEPT
: seq{type_index<std::remove_cv_t<std::remove_reference_t<Type>>>::value()},
identifier{type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value()},
alias{type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()} {
ENTT_ASSERT(seq != 0u, "Invalid type index");
}
alias{type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()} {}
/**
* @brief Type index.
@@ -182,14 +173,6 @@ struct type_info final {
return alias;
}
/**
* @brief Returns true if properly initialized, false otherwise.
* @return True if properly initialized, false otherwise.
*/
[[nodiscard]] explicit constexpr operator bool() const ENTT_NOEXCEPT {
return (seq != 0u);
}
private:
id_type seq;
id_type identifier;

View File

@@ -84,16 +84,6 @@ TEST(TypeInfo, Functionalities) {
ASSERT_EQ(other.name(), info.name());
}
TEST(TypeInfo, Validity) {
entt::type_info info{};
entt::type_info other = entt::type_id<int>();
ASSERT_FALSE(info);
ASSERT_TRUE(other);
ASSERT_EQ(info.index(), 0u);
ASSERT_NE(other.index(), 0u);
}
TEST(TypeInfo, Order) {
entt::type_info rhs = entt::type_id<int>();
entt::type_info lhs = entt::type_id<char>();