0

I have checked out this and other similar questions, but all the answer says that I need to use a template. However, let's say I want to implement typeid(T) from the standard library for fun. How would I be able to do that?

I tried to track down the typeinfo header in libstdc++'s repository, and I think I found the code for typeinfo, but I cannot find the constructor of typeinfo that takes type as a parameter. Where is the implementation of typeinfo that takes type as a parameter, or how do I use type as a parameter, just like how typeid did it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leorex
  • 2,058
  • 1
  • 14
  • 15
  • 2
    There are no constructors for `typeinfo` or derived classes. All instances are static const and compiler-generated, which makes sense if you think about it: C++ does not have any means to define a type at runtime. Also, builtin operators can have behaviors you cannot duplicate with other language constructs. – Deduplicator Aug 08 '14 at 11:57
  • so basically I cannot do something similar to typeid(T) because it is a built-in operator, just like sizeof(T)? :( Thanks, I will close the question soon. – leorex Aug 08 '14 at 12:08
  • Yes. (Also keep in mind that `T` could be a variable instead of a type there) – Deduplicator Aug 08 '14 at 12:11
  • You may use a macro which turns `my_typeid(T)` into something similar to `my_typeid_impl`. – Jarod42 Aug 08 '14 at 12:28

0 Answers0