Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the random access property of iota_view depend on the element type? And how to write a Foo such that iota(Foo{1}, Foo{10}) is random-access?

The following code fails to compile if you uncomment the commented line. (Compiler Explorer)

Why is that?

I imagine the reason is that the iota_view of Foos doesn't know how to advance a Foo. But how do I make it know?

#include <iostream>
#include <ranges>

struct Foo {
    int x;
    Foo() : x{-1} {}
    Foo(int x) : x{x} {}
    using difference_type = int;
    friend auto operator<=>(Foo const& a, Foo const& b) {
        return a.x <=> b.x;
    }
    friend bool operator==(Foo const& a, Foo const& b) {
        return a.x == b.x;
    }
    auto& operator++() {
        ++x;
        return *this;
    }
    auto operator++(int) {
        auto r = *this;
        ++x;
        return r;
    }
};

int main () {
    auto ints = std::views::iota(1, 30);
    for (auto i : ints) {}
    std::cout << ints[15] << std::endl;
    auto foos = std::views::iota(Foo{1}, Foo{30});
    for (auto i : foos) {}
    //std::cout << foos[15] << std::endl;
}

The example uses std-ranges, but I'm interested in an answer for range-v3 too, should they differ.

like image 553
Enlico Avatar asked Feb 02 '26 21:02

Enlico


1 Answers

Take a look at this page: https://en.cppreference.com/w/cpp/ranges/iota_view/iterator

To be a random access range, the type needs to be sufficiently "advanceable". This requires operator+, operator+=, operator- and operator-= (as well as operator-- to be "decrementable", and to be totally ordered which you already have with operator<=>)

Advancing an iterator by n will just add n to the current value with operator+= or operator+.

Example implementation:

struct Foo {
    // ...
    Foo& operator--() { --x; return *this; }
    Foo operator--(int) { auto r = *this; --*this; return r; }
    friend Foo operator+(Foo l, difference_type r) { l += r; return l; }
    friend Foo operator+(difference_type l, Foo r) { r += l; return r; }
    friend Foo operator-(Foo l, difference_type r) { l -= r; return l; }
    friend difference_type operator-(Foo l, Foo r) { return l.x - r.x; }
    Foo& operator+=(difference_type r) { x += r; return *this; }
    Foo& operator-=(difference_type r) { x -= r; return *this; }
};
like image 163
Artyer Avatar answered Feb 05 '26 11:02

Artyer