Skip to content

Instantly share code, notes, and snippets.

@HunterKohler
Created December 2, 2021 01:52
Show Gist options
  • Save HunterKohler/924fd0baf2284d03b46f149799e84bcf to your computer and use it in GitHub Desktop.
Save HunterKohler/924fd0baf2284d03b46f149799e84bcf to your computer and use it in GitHub Desktop.
STL Container iterator wrapper
/*
* Wraps the template parameter without changing any semantics. Good
* for wrapping pointers.
*/
template <typename Iterator>
class normal_iterator {
public:
using difference_type = typename std::iterator_traits<Iterator>::difference_type;
using value_type = typename std::iterator_traits<Iterator>::value_type;
using pointer = typename std::iterator_traits<Iterator>::pointer;
using reference = typename std::iterator_traits<Iterator>::reference;
using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;
normal_iterator() : _base() {}
normal_iterator(const Iterator &it) : _base{ it } {}
normal_iterator(const normal_iterator &it) : _base{ it.base() } {}
pointer operator->() { return _base; }
reference operator*() const { return *_base; }
normal_iterator operator[](difference_type n) const { return _base[n]; }
normal_iterator operator+(difference_type n) const { return _base + n; }
normal_iterator operator-(difference_type n) const { return _base - n; }
normal_iterator operator++(int) { return _base++; }
normal_iterator operator--(int) { return _base--; }
normal_iterator &operator++() { return _base++, *this; }
normal_iterator &operator--() { return _base--, *this; }
normal_iterator &operator+=(difference_type n) { return _base += n, *this; }
normal_iterator &operator-=(difference_type n) { return _base -= n, *this; }
bool operator==(const normal_iterator &it) const { return _base == it->_base; }
bool operator<(const normal_iterator &it) const { return _base < it->_base; }
bool operator>(const normal_iterator &it) const { return _base > it->_base; }
bool operator<=(const normal_iterator &it) const { return _base <= it->_base; }
bool operator>=(const normal_iterator &it) const { return _base >= it->_base; }
Iterator &base() { return _base; };
private:
Iterator _base;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment