I need to key some data in a map by a type. Currently I have something like this:
struct TypeInfoComparer
{
bool operator()(std::type_info const* a, std::type_info const* b) const
{
return a->before(*b);
};
};
std::map<std::type_info const*, Foo, TypeInfoComparer> d_fooByTypeId;
Which I can then look up from using (for example, in a template method having <typename T>
:
auto pair = d_fooByTypeId.find(&typeid(T));
However today I was reading about std::type_index
which seems to be intended for use in such a case as this.
I'm interested in improving my C++ knowledge. Can someone please explain whether I should modify my code to use std::type_index
, and why? Is there a reason beyond being able to remove the TypeInfoComparer
?
type_info
returned bytypeid
is persistant and unique for a given type? – Collegiumbefore
member function in his comparator. I don't know why everyone misses that fact. – Burgle