fork download
  1. #include <typeinfo>
  2. #include <bitset>
  3. #include <map>
  4. #include <type_traits>
  5.  
  6. class Component {};
  7.  
  8. class ComponentA : public Component {};
  9. class ComponentB : public Component {};
  10. class NonComponent {};
  11.  
  12. constexpr int BITSIZE = 10;
  13.  
  14. class ComponentType {
  15. public: std::bitset<BITSIZE> getBit() { return std::bitset<BITSIZE>(); }
  16. };
  17.  
  18. namespace {
  19. std::map<std::size_t,ComponentType*> componentTypes;
  20.  
  21. ComponentType &getTypeFor(const std::type_info &t){
  22. ComponentType *type = componentTypes[t.hash_code()];
  23.  
  24. if(!type)
  25. {
  26. type = new ComponentType();
  27. componentTypes[t.hash_code()] = type;
  28. }
  29.  
  30. return *type;
  31.  
  32. };
  33.  
  34. template<typename T>
  35. typename std::enable_if<std::is_base_of<Component,T>::value,
  36. std::bitset<BITSIZE>>::type
  37. getBit(const T &t) {
  38. return getTypeFor(typeid(t)).getBit();
  39. }
  40.  
  41. }
  42.  
  43. int main() {
  44. ComponentA a;
  45. getBit(a);
  46.  
  47. NonComponent n;
  48. getBit(n); // error, won't compile
  49. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'ComponentType&<unnamed>::getTypeFor(const std::type_info&)':
prog.cpp:22:48: error: 'const class std::type_info' has no member named 'hash_code'
prog.cpp:27:30: error: 'const class std::type_info' has no member named 'hash_code'
prog.cpp: In function 'int main()':
prog.cpp:48:13: error: no matching function for call to 'getBit(NonComponent&)'
stdout
Standard output is empty