提问人:zhaomin 提问时间:8/3/2021 最后编辑:zhaomin 更新时间:8/3/2021 访问量:60
使用具有动态多态性的范围的困难
difficulties in using ranges with dynamic polymorphism
问:
假设如下
// 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;
};
我通常在处理范围类型时使用,但显然这不适用于动态多态性。auto
有没有一种简单的方法来定义类型?也许?RangeOfInts
boost::any_range
或者关于如何改进这里的设计的任何建议?
答: 暂无答案
评论
virtual void foreach(std::function<void(int)>) = 0;
any_view<T>