Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++11 provide hashing functions for std::type_info?

I'm still working on a good solution to my One-Of-A-Type Container Problem -- and upon reflection I think it would be nice to be able to just use something like a std::map<std::type_info, boost::any>. Unfortunately, std::type_info does not define an operator<, and I think it'd be unreasonable for it to define one.

However, it does seem reasonable to define a hash function for it, because you could simply use the singleton address of the std::type_info object as a reasonable "hash". Therefore, you'd be able to put a std::type_info into a std::unordered_map as the key.

Does C++11 provide such a hash function? Would using the memory address of the std::type_info singleton be a bad hash strategy?

like image 938
Billy ONeal Avatar asked Aug 23 '10 22:08

Billy ONeal


1 Answers

You could also use type_index, it safely holds a pointer to a type_info, it's copyable, comparable and a hash function is provided for standard containers.

like image 72
Fericelli Avatar answered Sep 30 '22 19:09

Fericelli