I wanna write a container with random-access-iterator:
#include <cstddef>
#include <iterator>
#include <concepts>
namespace foo
{
struct container
{
struct iter
{
using difference_type = std::ptrdiff_t;
using pointer = int*;
using reference = int&;
using value_type = int;
using iterator_category = ::std::random_access_iterator_tag;
iter& operator++();
iter operator++(int);
iter& operator--();
iter operator--(int);
iter operator+(difference_type) const;
iter operator-(difference_type) const;
iter& operator+=(difference_type);
iter& operator-=(difference_type);
bool operator==(const iter&) const;
bool operator!=(const iter&) const;
bool operator<(const iter&) const;
bool operator>(const iter&) const;
bool operator<=(const iter&) const;
bool operator>=(const iter&) const;
difference_type operator-(const iter&) const;
int& operator*() const;
int& operator[](difference_type) const;
};
// code for reverse iterator, now it is comment out
/* using riter = std::reverse_iterator<iter>;
iter end();
riter rbegin()
{
return std::reverse_iterator(this->end());
}
*/
};
inline container::iter operator+(container::iter::difference_type, const container::iter&);
}
int main(void)
{
static_assert(std::random_access_iterator<foo::container::iter>, "");
return 0;
}
Then I compile it with g++-11 -std=c++20 and it can be compiled successfully.
But if I want to write a reverse iterator for this container, that is, uncomment those code for reverse iterator, there will be a compile error:
<source>:55:2: error: static_assert failed ""
static_assert(std::random_access_iterator<foo::container::iter>, "");
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:55:21: note: because 'foo::container::iter' does not satisfy 'random_access_iterator'
static_assert(std::random_access_iterator<foo::container::iter>, "");
^
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/iterator_concepts.h:669:8: note: because '__n + __j' would be invalid: invalid operands to binary expression ('const iter_difference_t<foo::container::iter>' (aka 'const long') and 'const foo::container::iter')
{ __n + __j } -> same_as<_Iter>;
^
1 error generated.
And I also use Clang 13 but got the same result. So how should I write the iterator correctly?