Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created November 5, 2021 14:58
Show Gist options
  • Save HalCanary/c071f21ac22552e7372a81f07946f950 to your computer and use it in GitHub Desktop.
Save HalCanary/c071f21ac22552e7372a81f07946f950 to your computer and use it in GitHub Desktop.
Example of c++17ish Iterator pattern.
#include <cstdio>
using Bar = int;
class Foo {
public:
struct Iter {
Iter& operator++() { ++ptr; return *this; }
bool operator!=(Iter const& other) const { return ptr != other.ptr; }
Bar& operator*() { return *ptr; }
Bar* ptr;
};
Iter begin() { return Iter{&array_[0]}; }
Iter end() { return Iter{&array_[N]}; }
private:
static constexpr unsigned N = 5;
Bar array_[N] = {1, 2, 3, 4, 5};
};
int main() {
for (Bar value : Foo()) {
printf("%d ", value);
}
puts("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment