I'm just a simple coder who used Python a lot and got addicted to its generators. As far as I understand the current situation, they can be cleanly implemented in C++20 with coroutines, however, at least until C++23, it's not a trivial task since one needs to write a generator class (template). How do I get one that
for
, the ranges library and some equivalent of Python's next
on it. It would also be great if there was a method to test whether the generator is exhausted.Is this possible at all?
As mentioned in the comment, libcoro provides a higher level of abstraction and might solve some of your issues.
For point2, if you really need a public method that tells you the generator is exhausted, I guess enhancing libcoro generator would make it somehow easy. Here is a (not tested) possible sample. But is checking against generator.end() a problem for you?
namespace libcoro {
template<typename T>
class [[nodiscard]] generator
{
public:
using reference_type = std::conditional_t<std::is_reference_v<T>, T, T&>;
//.. libcoro stuff
// ADDED
bool done() const {
m_coroutine.done();
}
reference_type value() const {
return m_coroutine.promise().value();
}
void resume() {
m_coroutine.resume();
}
// ...
};
}
Then you can do:
while (true) {
gen.resume();
if(gen.done()) {
std::cout << "this is the end!" << std::endl;
break;
}
std::cout << "new value: " << gen.value() << std::endl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With