Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1.  
  2.     #include <cstdio>
  3.     #include <cstdint>
  4.  
  5. template<typename T>
  6. struct EnumValue {
  7.     EnumValue()
  8.         : value(nextValue++)
  9.     {}
  10.     EnumValue(uint32_t value)
  11.         : value(value)
  12.     {
  13.         nextValue = value + 1;
  14.     }
  15.  
  16.     uint32_t value;
  17.  
  18.     static uint32_t nextValue;
  19. };
  20. template<typename T>
  21. uint32_t EnumValue<T>::nextValue = 0;
  22.  
  23. struct Animal {
  24.     static EnumValue<Animal> Tiger;
  25.     static EnumValue<Animal> Jaguar;
  26.     static EnumValue<Animal> Hyppo;
  27.     static EnumValue<Animal> Zebra;
  28. };
  29. EnumValue<Animal> Animal::Tiger;
  30. EnumValue<Animal> Animal::Jaguar;
  31. EnumValue<Animal> Animal::Zebra;
  32. EnumValue<Animal> Animal::Hyppo = 69U;
  33.    
  34.  
  35.  
  36. int main()
  37. {
  38.     printf("Tiger\t%d\n", Animal::Tiger.value);
  39.     printf("Jaguar\t%d\n", Animal::Jaguar.value);
  40.     printf("Hyppo\t%d\n", Animal::Hyppo.value);
  41.     printf("Zebra\t%d\n", Animal::Zebra.value);
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement