Created
November 21, 2022 12:58
-
-
Save erinacio/8084de41713a722860e3b4fa59f624c7 to your computer and use it in GitHub Desktop.
Demonstrate how to make your own iterator type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
[[noreturn]] void TODO() { abort(); } | |
struct output_iterator { | |
using difference_type = ptrdiff_t; | |
int &operator*() { TODO(); } | |
output_iterator &operator++() { TODO(); } | |
output_iterator operator++(int) { TODO(); } // NOLINT(cert-dcl21-cpp) | |
}; | |
static_assert(std::output_iterator<output_iterator, int>); | |
template <typename T> | |
struct input_iterator_impl { | |
using difference_type = ptrdiff_t; | |
using value_type = int; | |
const int &operator*() const { TODO(); } | |
T &operator++() { TODO(); } | |
T operator++(int) { TODO(); } // NOLINT(cert-dcl21-cpp) | |
}; | |
struct input_iterator : input_iterator_impl<input_iterator> {}; | |
static_assert(std::input_iterator<input_iterator>); | |
template <typename T> | |
struct forward_iterator_impl : input_iterator_impl<T> { | |
friend bool operator==(const T &, const T &) { TODO(); } | |
friend bool operator!=(const T &, const T &) { TODO(); } | |
}; | |
struct forward_iterator : forward_iterator_impl<forward_iterator> {}; | |
static_assert(std::forward_iterator<forward_iterator>); | |
template <typename T> | |
struct bidirectional_iterator_impl : forward_iterator_impl<T> { | |
T &operator--() { TODO(); } | |
T operator--(int) { TODO(); } // NOLINT(cert-dcl21-cpp) | |
}; | |
struct bidirectional_iterator | |
: bidirectional_iterator_impl<bidirectional_iterator> {}; | |
static_assert(std::bidirectional_iterator<bidirectional_iterator>); | |
template <typename T> | |
struct random_access_iterator_impl : bidirectional_iterator_impl<T> { | |
friend std::strong_ordering operator<=>(const T &, const T &) { TODO(); } | |
T &operator+=(ptrdiff_t) { TODO(); } | |
T &operator-=(ptrdiff_t) { TODO(); } | |
friend T operator+(const T &, ptrdiff_t) { TODO(); } | |
friend T operator+(ptrdiff_t, const T &) { TODO(); } | |
T operator-(ptrdiff_t) const { TODO(); } | |
ptrdiff_t operator-(const T &) const { TODO(); } | |
const int &operator[](ptrdiff_t) const { TODO(); } | |
}; | |
struct random_access_iterator | |
: random_access_iterator_impl<random_access_iterator> {}; | |
static_assert(std::random_access_iterator<random_access_iterator>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment