SilverTES

Unique ID generated by type in runtime in C++.

Jan 6th, 2021 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. const int NO_INDEX = -1;
  2.  
  3. template <typename T>
  4. struct StaticType
  5. {
  6.     static int type;
  7. };
  8. template <typename T> int StaticType<T>::type = NO_INDEX;
  9. static int _uniqueType = 0;
  10.  
  11. struct UID
  12. {
  13.     template <typename T>
  14.     static int Get()
  15.     {
  16.         if (StaticType<T>::type != NO_INDEX)
  17.         {
  18.             return StaticType<T>::type;
  19.         }
  20.         else
  21.         {
  22.             ++_uniqueType;
  23.             StaticType<T>::type = _uniqueType;
  24.         }
  25.  
  26.         return StaticType<T>::type;
  27.     }
  28.  
  29. };
Add Comment
Please, Sign In to add comment