fork download
  1. #include <iostream>
  2.  
  3. namespace
  4. {
  5. enum Block_Edges
  6. {
  7. FROM = 0,
  8. TO = 4,
  9. };
  10.  
  11. Block_Edges& operator++(Block_Edges& e)
  12. {
  13. return e = static_cast<Block_Edges>(e + 1);
  14. }
  15.  
  16. }
  17.  
  18. template<typename T, bool enable = std::is_integral<T>::value || std::is_enum<T>::value>
  19. struct range_impl
  20. {
  21. struct iterator
  22. {
  23. T operator * ()const noexcept
  24. {
  25. return value;
  26. }
  27.  
  28. iterator& operator ++() noexcept
  29. {
  30. ++value;
  31. return *this;
  32. }
  33.  
  34. friend bool operator != (const iterator& lhs, const iterator& rhs) noexcept
  35. {
  36. return lhs.value != rhs.value;
  37. }
  38.  
  39. T value;
  40. };
  41.  
  42. std::size_t size() const
  43. {
  44. return last - first;
  45. }
  46.  
  47. const iterator begin()const noexcept
  48. {
  49. return{ first };
  50. }
  51.  
  52. const iterator end()const noexcept
  53. {
  54. return{ last };
  55. }
  56.  
  57. T first;
  58. T last;
  59. };
  60.  
  61. template<typename T>
  62. struct range_impl<T, false>
  63. {
  64. range_impl(T first, T last)
  65. : first(first)
  66. , last(last)
  67. {}
  68.  
  69. std::size_t size() const
  70. {
  71. return std::distance(first, last);
  72. }
  73.  
  74. const T begin()const noexcept
  75. {
  76. return{ first };
  77. }
  78.  
  79. const T end()const noexcept
  80. {
  81. return{ last };
  82. }
  83.  
  84. T first;
  85. T last;
  86. };
  87.  
  88. template<typename T>
  89. range_impl<T> range(T first, T last) noexcept
  90. {
  91. return{ first, last };
  92. }
  93.  
  94. int main()
  95. {
  96. for (const auto& i : range(Block_Edges::FROM, Block_Edges::TO))
  97. {
  98. std::cout << '\n' << i << ':';
  99. for (const auto& j : range(Block_Edges::FROM, Block_Edges::TO))
  100. {
  101. std::cout << j << ' ';
  102. }
  103. }
  104. }
  105.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
0:0 1 2 3 
1:0 1 2 3 
2:0 1 2 3 
3:0 1 2 3