#include <type_traits>
#include <string>
#include <array>

enum class ETest : uint8_t {
  kT1,
  kT2,
};

struct cursor {
  bool next(uint8_t& slot) {
    if (index >= length) {
      return false;
    }

    slot = array[index++];
    return true;
  }

  void rewind() {
    index = 0;
  }

  uint8_t array[2] {22,32};
  uint8_t length = 2;
  uint8_t index = 0;
};

template <class ...Args>
class cursor2 {
public:
  bool next(uint8_t& slot) {
    if (index >= length) {
      return false;
    }

    slot = array[index++];
    return true;
  }

  void rewind() {
    index = 0;
  }

  template <class T>
  static auto CheckArg(T t)
  {
    static_assert(sizeof(t) == sizeof(uint8_t), "sizeof(type) check faild.");
    return static_cast<uint8_t>(t);
  }

  cursor2(Args... args)
  {
     std::array<uint8_t , sizeof...(Args)> a = {CheckArg(args)...};
     array.swap(a);

     index = 0;
     length = array.size();
  }

private:
  std::array<uint8_t , sizeof...(Args)> array;
  uint8_t index;
  uint8_t length;
};

template <typename T1>
class cursor3 {
private:
   using Type = typename std::underlying_type<T1>::type;
public:
  template <class T>
  static auto CheckArg(T t)
  {
    static_assert(sizeof(t) == sizeof(Type), "sizeof(type) != 1");
    return static_cast<Type>(t);
  }

  template <class ...Args>
  static auto create(Args... args)
  {
    std::array<Type , sizeof...(args)> array = {CheckArg(args)...};
    return array;
  }

private:
  cursor3();
};

int main() {

  auto a = cursor3<ETest>::create(ETest::kT1, ETest::kT2);
  printf("sizeof...%d\n", sizeof(a));

  for (auto begin = a.begin(); begin < a.end(); ++begin) {
        printf("%d\n", *begin);
  }

//  auto cur = cursor2<ETest,ETest>(ETest::kT1, ETest::kT2);
//  uint8_t val;
//
//  while (cur.next(val)) {
//    printf("%d\n", val);
//  }

//  auto cur = cursor();
//  uint8_t val;
//
//  while (cur.next(val)) {
//    printf("%d\n", val);
//  }
//
  return 0;
}


备份地址: 【通过模板实现可迭代cursor类