3 ms·
> How do you go about Id<T>? The way I see that something like this can work out and give small advantages is that T would be an abstract type (a phantom or tag
by jcelerier 6y ago
> How do you go about Id<T>? The way I see that something like this can work out and give small advantages is that T would be an abstract type (a phantom or tag type, or whatever C++ guys are calling it).
in a very simplified way:
template<typename T>
struct Id { int value; };
template<typename T>
struct Identifiable { Id<T> id; };
struct MyEntity : Identifiable<MyEntity> { string name; void foobar(); };
> And all arrays that are meant to be indexed using Id<T> (while the array element types could be whatever) would depend on this type and have to be implemented using a special wrapped array type with overloaded operator[].
std::unordered_map<Id<T>, whatever>
works fine (or your container of choice... flat_map, boost::multi_index...).
> It worked wonderfully. I had maybe 1 mistake where I submitted the wrong "type" in 6 months.
well, you're good, I remember when I introduced this in my code base, that caught a dozen bugs almost immediately - and I get prevented by the compiler to introduce more every couple months.