get_component_type_id<BaseClass>
and get_component_type_id<ChildClass>
are two different functions. Hence, you get two of static size_t uniqueComponentId
, each with their own value.
Update, in response to comment by OP
Yes, it is possible. You could use:
template <typename T>
inline std::size_t get_component_type_id(T*, std::false_type) noexcept
{
static size_t uniqueComponentId{__INTERNAL__::getUniqueComponentId()};
return uniqueComponentId;
}
inline std::size_t get_component_type_id(BaseClass*, std::true_type) noexcept
{
static size_t uniqueComponentId{__INTERNAL__::getUniqueComponentId()};
return uniqueComponentId;
}
template<typename T>
inline std::size_t get_component_type_id() noexcept
{
static_assert(std::is_base_of<Component, T>::value, "T must be of type Component.");
return get_component_type_id((T*)nullptr, typename std::is_convertible<T, BaseClass>::type{});
}
However, it is fragile. If you want the same behavior for another class derived from Component
, you will need to make substantial changes.
You will be better off using a virtual
member function.
struct Component
{
virtual size_t get_type_id() const = 0;
};
struct BaseClass : Component
{
size_t get_type_id() const
{
static size_t uniqueComponentId{__INTERNAL__::getUniqueComponentId()};
return uniqueComponentId;
}
};
struct ChildClass : BaseClass {};
Now you can implement size_t get_type_id() const
at any level in the inheritance hierarchy as you see fit.