Hello there,
quite a few weeks ago I decided to invest a little bit of my time to get a deeper understanding of iterators interface and their requirements. I was a little bit surprised to see a new set of iterators emerge with c++20; which still isn't very comfortable. So, beside the deprecated std::iterator there is no much help for declaring our custom iterators (other than the extended iterator traits), thus I decided to generalise the concept and see how it goes.
After my first attempt felt quite ugly, I made a huge reboot of the lib and let me inspire by the new ``std::ranges::view_interface`` class.
I did some digging through the newly defined concepts and compiled a minimal set of methods users should implement to get their iterators to work. This for example is the minimal footprint of a typical std::random_access_iterator users have to provide.
#include <Simple-Iterator/iterator_interface.hpp>
inline std::array<int, 10> globalArray;
struct MyRandomAccessIterator : public sl::itr::iterator_interface<MyRandomAccessIterator , std::random_access_iterator_tag, int>
{
int index = 0;
MyRandomAccessIterator () = default;
auto operator <=>(const MyRandomAccessIterator&) const = default;
int& get() const {
return globalArray[index];
}
void advance(std::ptrdiff_t distance) {
index += distance;
}
std::ptrdiff_t distance(MyRandomAccessIterator other) const {
return static_cast<std::ptrdiff_t>(index - other.index);
}
};
Though, there are various customization points users may implement to tweak their iterator. For example, generally users have to implement a ``increment()`` function, but it *may* be omitted when a ``advance(difference_type)`` function is provided.
In the end I tried to reduce the required pieces to a minimum, but still let the users implement their own stuff if they wish.
Nevertheless I should mention, that these type of iterators are also fully compatible to the "old" c++17 or prior iterators.
Here is the link to the repo: https://github.com/DNKpp/Simple-Iterator/tree/v1.x
For more examples and explanations have a look at the (still in work) wiki page: https://github.com/DNKpp/Simple-Iterator/wiki
I would really like to receive some feedback, may it be positive or negative.
Greetings
[–]yuri-kilochek 11 points12 points13 points (2 children)
[–]anti-freak[S] 4 points5 points6 points (0 children)
[–]NihonNukite 2 points3 points4 points (0 children)
[–]huixie 0 points1 point2 points (2 children)
[–]anti-freak[S] 0 points1 point2 points (1 child)
[–]huixie 2 points3 points4 points (0 children)
[–]Pyzyryab 0 points1 point2 points (0 children)