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 Foo
s 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 usees std-ranges, but I'm interested in an answer for range-v3 too, should they differ.