Suppose the following
// interface that provides a range of ints
struct C
{
virtual RangeOfInts foreach() const = 0;
};
struct B
{
void g(const C& c_) {
for (int i : c_.foreach()) {
// do something
}
}
};
struct A
{
struct C1 : public C
{
// implementation of C with A specific info, for example
RangeOfInts foreach() const override {
return v | boost::adaptors::transformed([](int i){return i*2;});
}
std::vector<int> v;
};
void f() {
b.g(c1);
}
B b;
C1 c1;
};
I'd usually use auto
when dealing with range types, but clearly that won't work for dynamic polymorphism.
Is there an easy way to define the type RangeOfInts
? Perhaps boost::any_range
?
Or any suggestions on how I can improve the design here?