fork(4) download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <string>
  4. #include <map>
  5. using namespace std;
  6.  
  7. // Quick proof of concept (to be improved)
  8. map<const type_info*, string> mytypes;
  9.  
  10. template <class T>
  11. const string& mytype(T &&x) {
  12. return mytypes[&typeid(x)];
  13. }
  14.  
  15. // Some test types
  16. struct A { virtual int test() {} };
  17. struct B : A {};
  18.  
  19. int main() {
  20. // To be improved: initialization of the type info extension
  21. mytypes[&typeid(int)]="Plain C++ integer";
  22. mytypes[&typeid(A)]="Structure A";
  23. mytypes[&typeid(B)]="Structure B";
  24.  
  25. // demo, including polymorphic type
  26. int a;
  27. A * p = new B;
  28. cout << typeid(int).name() <<endl;
  29. cout << mytype(a) <<endl;
  30. cout << mytype(*p) <<endl;
  31. return 0;
  32. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
i
Plain C++ integer
Structure B