18

According to cplusplus.com, the std::type_info::before() function...

Returns true if the type precedes the type of rhs in the collation order.
The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order.

So what is it useful for?

4 Answers 4

27

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";
}
5
  • 1
    "The type_info doesn't have an operator < defined, so you must provide your own comparator." std::less<> (std::map<>'s default comparitor) works for all pointers, you just get address ordering instead of collation ordering.
    – ildjarn
    Dec 30, 2011 at 18:34
  • 7
    @ildjarn: you misunderstand the problem. The standard doesn't guarantee that at most one type_info per type exists. Infact it's common to encounter more than one typeinfo created for the same type. The most trivial case is in context of dynamically-linked libraries, like Dietmar said. Dec 30, 2011 at 19:01
  • 1
    @ildjarn Reread your quotation. It talks about operator < of type_info, but you argue about validity of comparing pointers to type_info, implying that the quoted sentence is somehow wrong. But it is not, because I did not say anything about comparing pointers. Then why you people write irrelevant comments? Aug 20, 2014 at 9:12
  • I don't understand what &typeid(type) does. It obtains the address of the object typeid(int) in this example? But what then is typeid(int) - I assumed this would return an integer, like sizeof, but this doesn't seem to make sense when then taking the address of that object... Could you perhaps add some explanation of what this line of code in the example does? Aug 4, 2017 at 12:35
  • @user3728501: it returns a statically allocated object of type type_info. Aug 4, 2017 at 12:47
8

This is useful to define an order on typeinfo objects, e.g. to put them into a std::map. The obvious follow-up question is: why isn't it spelled operator<()? I don't know the answer to this question.

9
  • 2
    "The obvious follow-up question is: why isn't it spelled operator<()?" How would one define an operator < for a pointer type?
    – ildjarn
    Dec 30, 2011 at 18:36
  • type_info has no copy constructor or copy-assignment operator, so it can't be stored directly into a container. And of course, the key type of an associative container cannot be a reference, so the only remaining option is to store a type_info*, for which one cannot supply an operator<. Possibly the sensible thing to do is to specialize std::less<type_info*>.
    – ildjarn
    Dec 30, 2011 at 18:55
  • 1
    @ildjarn: Dietmar's question is 'why type_info has before() but not operator<'. Although what you say is correct, it doesn't answer his question. E.g. why can't I write typeid(int) < typeid(long), instead I have to use typeid(int).before(typeid(long)). Dec 30, 2011 at 19:04
  • 5
    @DietmarKühl: note that C++11 defines type_index that is just a wrapper for const type_info* with all the comparison operators defined. Dec 30, 2011 at 21:09
  • 1
    @YakovGalka I looked at llvm implementation of type_index. The constructor takes reference of type_info but the internal private field which "holds" it is a pointer to type_info object. Why they use Pointer instead of Reference internally? github.com/llvm/llvm-project/blob/main/libcxx/include/…
    – x4444
    Dec 18, 2020 at 2:06
1

It gives an ordering.

That is required if you want to store values in some containers, like std::map.

1

Think of it as less-than (<) operator for type_info objects. If you ever wanted to store in ordered collection - such a set of map - you can use it to make an appropriate comparator. It's a reliable and preferred way, as opposed to, say, using type's name which might not be unique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.