Search code examples
c++rttitypeid

typeid results not matching


I have function:

 void func(unsigned int event) {
       printf("%u %u\r\n", typeid(event), typeid(unsigned int&));
    // prints 5338164 0
       printf("%u %u\r\n", typeid(event), typeid(unsigned int));
    // prints 21525556 0
}

Assume this question is RTFM-related, but i reread typeid() man page several times and could not find an answer. What is wrong with this code? Why %u's is not equal? also why it changes first %u when I changed 2nd param in code. Thanks.


Solution

  • Try

    printf("%s %s\n", typeid(event).name(), typeid(unsigned int).name());
    

    As previous replies said, the result of typeid() is opaque and cannot be printed as if it was an int. The fact that this code compiles is just a result of C's bad typing.