I wanted to verify that typeid is evaluated at compile time when used with a type name (i.e. typeid (int), typeid (std :: string) ...).
To do this, I repeated the comparison of two type calls in a loop and compiled it with the optimizations turned on to make sure that the compiler simplified the loop (by looking at the runtime of 1us when it simplifies 160 ms, if it is not).
And I get strange results, because sometimes the compiler simplifies the code, and sometimes not. I am using g ++ (I tried different versions of 4.x) and here is the program:
#include <iostream> #include <typeinfo> #include <time.h> class DisplayData {}; class RobotDisplay: public DisplayData {}; class SensorDisplay: public DisplayData {}; class RobotQt {}; class SensorQt {}; timespec tp1, tp2; const int n = 1000000000; int main() { int avg = 0; clock_gettime(CLOCK_REALTIME, &tp1); for(int i = 0; i < n; ++i) { // if (typeid(RobotQt) == typeid(RobotDisplay)) // (1) compile time // if (typeid(SensorQt) == typeid(SensorDisplay)) // (2) compile time if (typeid(RobotQt) == typeid(RobotDisplay) || typeid(SensorQt) == typeid(SensorDisplay)) // (3) not compile time ???!!! avg++; else avg--; } clock_gettime(CLOCK_REALTIME, &tp2); std::cout << "time (" << avg << "): " << (tp2.tv_sec-tp1.tv_sec)*1000000000+(tp2.tv_nsec-tp1.tv_nsec) << " ns" << std::endl; }
The conditions under which this problem appears are unclear, but: - if there is no inheritance, not a problem (always compilation time)
- if I make only one comparison, not a problem - the problem arises only with the disjunction of comparisons, if all members are false
So is there something that I could not handle, how typeid works (should it always be evaluated at compile time when used with type names?), Or could it be a gcc error when evaluating or optimizing?
In the context, I traced the problem to this very simplified example, but my goal is to use typeid with template types (since specialization of partial function templates is not possible).
Thanks for your help!
source share