Context:
I am trying to build a container that will behave as a wrapper around a multi-dimensional array of run time defined dimensions - in fact the underlying array is of course a 1D array of the total size. The main part is that operator []
returns a wrapper on the sub array.
As containers need iterators, I am currently implementing iterators on that container, both Container::iterator
and Container::const_iterator
. I try hard to mimic standard container iterators, and my implementation respects almost all requirements for a random access iterator except for:
Forward iterators [forward.iterators]
...
6 Ifa
andb
are both dereferenceable, thena == b
if and only if*a
and*b
are bound to the same object.
The reason for not respecting it, is that operator *
has to return a reference to a sub-container. So my implementation is a stashing iterator that contains a sub-container member that moves with the iterator, and operator *
returns a reference to this member object
Over simplified implementation:
template <class T>
class SubArray {
T *arr;
size_t *sizes;
size rowsize;
public:
...
Iterator<T> begin() {
return Iterator<T>(operator[](0));
}
...
};
class Iterator<T> {
SubArray elt;
public:
Iterator(const SubArray<T>& pos): elt(pos) {}
...
SubArray<T>& operator *() {
return elt;
...
};
According to cppreference (and to source of different implementations) std::filesystem::path::iterator
is also a stashing iterator.
Question
The iterator_category
member of an iterator is supposed to help other classes to identify the type of an iterator from input iterator to random access. What should be the iterator_category
of my stashing iterator, that fulfills almost all requirements for a random access iterator but fails for one point of forward iterator? Note: this is enough to make std::reverse_iterator
not usable on it.
Remark:
I can confirm that std::filesystem::path::iterator
contains for Clang implementation:
typedef bidirectional_iterator_tag iterator_category;
but also a marker to prevent std::reverve_iterator
to try to use it
References:
- full source for my container class on Code Review
- question about
std::reverse_iterator
on a stashing container on SO std::reverse_iterator
on cppreference (see the note above the example code)