Created
November 5, 2021 14:58
-
-
Save HalCanary/c071f21ac22552e7372a81f07946f950 to your computer and use it in GitHub Desktop.
Example of c++17ish Iterator pattern.
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 <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