So since it was introduced I have been loving the for each in keywords to iterate STL collections.(I’m a very very big fan of syntactic sugar).
My question is how can I write a custom collection that can be iterated using these keywords?
Essentially, what APi do I need to expose for my collections to be iterable using these keywords?
I apologize if this sounds blunt, but please do not respond with “use boost”, “don’t write your own collections”, or the like. Pursuit of knowledge, my friends. If it’s not possible, hey, I can handle that.
I’d also very much so prefer not to have to inject an STL iterator into my collections.
Thanks in advance!
Here is a good explanation of iterable data structures (Range-Based loops):
In order to make a data structure iterable, it must work similarly to the existing STL iterators.
beginandendmethods that operate on that structure,either as members or as stand-alone functions, and that return iterators to
the beginning and end of the structure.
operator*method, anoperator !=method, and anoperator++method, either as members or as stand-alone functions.Note, in
C++11there is an integrated support for range-based loops without the use ofSTL, though the above conditions hold for this as well. You can read about it at the same link above.