Skip to content

Instantly share code, notes, and snippets.

@craigweston
Created December 1, 2023 17:19
Show Gist options
  • Save craigweston/50d23ef1f122956423e757ab5cd1a5c9 to your computer and use it in GitHub Desktop.
Save craigweston/50d23ef1f122956423e757ab5cd1a5c9 to your computer and use it in GitHub Desktop.
C++ EnumIterator
template<typename C, C first, C last>
class EnumIterator {
private:
typedef typename std::underlying_type<C>::type val_t;
public:
EnumIterator(C const &f) : val(f) {}
EnumIterator() : val(first) {}
EnumIterator operator++() {
++val;
return *this;
}
EnumIterator operator++(int) {
auto temp = *this;
++val;
return temp;
}
C operator*() { return static_cast<C>(val); }
EnumIterator begin() { return *this; }
const EnumIterator end() const {
static const EnumIterator endIter = ++EnumIterator(last);
return endIter;
}
bool operator!=(EnumIterator const &i) { return val != i.val; }
private:
val_t val;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment