Somewhere during the forming of the new standard there was a promise for easier type_info usage, which among others resulted in the implementation of hash_code. Implementing a reflection system in C++11 however I ran into a problem with unique type identification again.
I need:
Note that I do not need persistence between program exeutions. As it seems:
.name() is completely useless, because the standard gives no guarantees on it whatsoever..hash_code() is also useless because it's not guaranteed to be uniquetype_info object won't work everywhere (across DLL's for example).before() seems to be useful - although I don't know if it wouldn't suffer from the same problem as #3Even if .before() is to be used, then we can use map, while I would prefer to use unordered_map.
struct compare_type_info {
bool operator ()(const type_info* a, const type_info* b) const {
return a->before(*b);
}
};
std::map<const type_info*, X, compare_type_info> map;
m[&typeid(int)] = something;
Is the above safe for collision? Does the ordering operator guarantee overwrite of !< and !> values?
Is there a way to solve this problem without hash collision risk?
In terms of rolling up my own type system, I already do that, but typeid solves problems with giving the proper final type from a base type (inheritance) and I do not want to add any fields to my classes (the type system is "external").
Even in C++11 are we still screwed? :/
You can use std::type_index, which is constructable from a std::type_info. These are fully ordered, implementing all of the relational operations. A type_index is even implicitly convertible from type_info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With