Previously it was mentioned that Python and C++ do iteration quite differently. Python has "statefull" objects that have a .next() method that returns a new object or throws a StopIteration exception. Incidentally Rust does exactly the same thing, except that it uses an optional type rather than an exception. C++ does not work like this, instead it has a concept of a "start" and "end" of a sequence and the machinery keeps incrementing the start until it reaches the end.
At the end of last post it was said that it is difficult to integrate an object of the former type with C++'s native language facilities, at least without macros.
Now we'll look how to integrate an object of the former type with C++'s native language facilities without any macros.
In fact, it only took fewer than 30 lines of code to do. The caveat being that it is probably fairly easy to break it. But it works for me.
Here it is being used.
There is also a second, similar helper class that takes ownership of the object being iterated over.
No comments:
Post a Comment