begin() / end() メソッドを実装するために、クラスの内部でビューを使用することは可能ですか?
たとえば、次のクラスを反復可能にしたいと考えています。反復ごと
op
に、2 つの iterable の現在の要素で呼び出されます。
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;
};
range-v3 のおかげでzip_with
ビューを使用できます (コードはテストされていません!)
ranges::view::zip_with(op, first, second);
しかし、このビューを使用して begin() / end() メソッドを実装できますか?
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();
}
};
2 つの反復子 (開始と終了) を安全に比較できますか?
私が達成したい最終結果は、任意の数の 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) { ... }