fork download
  1. #include <iostream>
  2.  
  3. template <unsigned SIZE>
  4. struct offset_size {
  5. typedef typename offset_size<SIZE - 1>::type type;
  6. };
  7.  
  8. template <>
  9. struct offset_size<0> {
  10. typedef unsigned char type;
  11. };
  12.  
  13. template <>
  14. struct offset_size<257> {
  15. typedef unsigned int type;
  16. };
  17.  
  18. template<unsigned SIZE>
  19. class circular_buffer {
  20. public: // Added this line so we can inspect the sizes from main()
  21. unsigned char buffer[SIZE];
  22. typename offset_size<SIZE>::type head; // index
  23. typename offset_size<SIZE>::type tail; // index
  24. };
  25.  
  26. int main() {
  27. std::cout << sizeof(circular_buffer<256>().head) << std::endl;
  28. std::cout << sizeof(circular_buffer<257>().head) << std::endl;
  29. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
1
4