#include <cinttypes>
#include <type_traits>
template<typename Id, typename Value>
class sparse_set {
static_assert(std::is_integral_v<Id>, ""); (1)
static_assert(std::is_unsigned_v<Id>, "");
Value& operator[](Id id);
void push_back(const Value& value);
// class implementation left out
};
class entity {
public:
explicit entity(std::uint32_t id) : _id(id) {}
~entity() = default;
std::uint32_t id() const {
return _id;
}
operator std::uint32_t() const { (2)
return _id;
}
private:
std::uint32_t _id;
}; // class entity
int main() {
const auto e = entity{2};
auto set = sparse_set<entity, int>{};
set.push_back(0);
set.push_back(1);
set.push_back(2);
set.push_back(3);
auto i = set[e]; (3)
return 0;
}
I am trying to use a class with a conversion operator to std::uint32_t (2) as an index into a container class (3).
Accessing an element with an instance of that class works and i get the right element.
But testing the class with a static_assert and std::is_unsigned_v and std::is_integral_v results in an assertion failure.
I need assertions to make sure Id can be used as an index.
When I static_assert with std::uint32_t everything works so I would expect the conversion operator to work aswell.
entity(Id) an integral type? I don't think so.static_assert(std::is_integral_v<entity>);would complain. "Provides the member constantvaluewhich is equal totrue, ifTis the typebool,char,char8_t(since C++20),char16_t,char32_t,wchar_t,short,int,long,long long, or any implementation-defined extended integer types, including any signed, unsigned, and cv-qualified variants. Otherwise, value is equal tofalse.". Also note: "The behavior of a program that adds specializations foris_integraloris_integral_v(since C++17) is undefined."