13 ms·
> Coroutines still seem overkill to me though. Coroutines are a fantastic way to implement ranges, in particular when you need runtime polymorphism over the w
by jcelerier 6y ago
> Coroutines still seem overkill to me though.
Coroutines are a fantastic way to implement ranges, in particular when you need runtime polymorphism over the way your range is implemented
e.g. they allow you to have for instance :
class foo {
public:
virtual coroutine_range_type<int> get_foobar_list() const = 0;
};
class foo_using_vector : public foo {
coroutine_range_type<int> get_list() const override {
for(int val : impl)
co_yield impl;
}
std::vector<int> impl;
};
class foo_using_some_fixed_list : public foo {
coroutine_range_type<int> get_list() const override {
co_yield 1;
co_yield 1;
co_yield 2;
co_yield 3; // or for instance getting something through some OS API, etc...
}
};
and then use that like you would use any container-like type, in range-based for-loops, in actual ranges...