Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <cstdint>
  2.  
  3. template <class... Ts>
  4. class type_list
  5. {
  6. private:
  7.   template <class U, class V, class... Vs>
  8.   struct type_id {
  9.     static constexpr int id() {
  10.       static_assert(sizeof...(Vs) != 0, "type not registered");
  11.       return type_id<U, Vs...>::id();
  12.     }
  13.   };
  14.  
  15.   template <class U, class... Vs>
  16.   struct type_id<U, U, Vs...> {
  17.     static constexpr int id() {
  18.       return sizeof...(Vs);
  19.     }
  20.   };
  21.  
  22. public:
  23.   template <class U>
  24.   static constexpr int id() {
  25.     return type_id<U, Ts...>::id();
  26.   }
  27. };
  28.  
  29. // Register types here
  30. using Types = type_list<int16_t, uint32_t>;
  31.  
  32. struct ElementInfo
  33. {
  34.   const char* name;
  35.   const int offset;
  36.   const int type;
  37. };
  38.  
  39. constexpr ElementInfo info[] = {
  40.   { "foo",    1, Types::id<uint32_t>() },
  41.   { "bar",    2, Types::id<int16_t>() },
  42. };
  43.  
  44. int main() {
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement