Consider you want to put your type_info
objects as keys into a map<type_info*, value>
. The type_info
doesn't have an operator <
defined, so you must provide your own comparator. The only thing that is guaranteed to work from the type_info
interface is the before()
function, since neither the addresses of type_info
nor the name()
must be unique:
struct compare {
bool operator ()(const type_info* a, const type_info* b) const {
return a->before(*b);
}
};
std::map<const type_info*, std::string, compare> m;
void f() {
m[&typeid(int)] = "Hello world";
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…