Is it possible to use a view internally to a class in order to implement the begin() / end() methods?
For example I want to make the following class iterable; at every iteration
op
is called on the current element of the two iterables.
template <typename It1, typename It2, typename Op>
struct binary_op {
binary_op(It1 const& f, It2 const& s, Op o): first{f}, second{s}, op{o} {}
It1 first;
It2 second;
Op op;
};
Thanks to range-v3 I can use the zip_with
view (code not tested!)
ranges::view::zip_with(op, first, second);
But can I implement the begin() / end() methods using this view?
using namespace ranges;
template <typename It1, typename It2, typename Op>
struct binary_op {
...
auto begin() const {
return view::zip_with(op, first, second).begin();
}
auto end() const {
return view::zip_with(op, first, second).end();
}
};
Can the two iterators (begin and end) safely compared?
The end result I want to achieve is the possibility to nest an arbitrary number of binary_op:
std::vector<int> v1, v2, v3;
auto r = binary_op(
binary_op(v1, v2, [](int a, int b) {return a + b;}),
v3,
[](int a, int b) {return a - b;});
for (auto x : r) { ... }