On stackoverflow I asked, what is the preferred way to expose custom STL-style iteration?
... In C#, I can do about the same with just a single function using
No, you really can't.
yield return looks similar to a Python generator, which in C++ terms is an InputIterator, the simplest of the six iterator categories described here.
Your original question implements a RandomAccessIterator, which is significantly more powerful than this - for example, you can do direct random-access binary search on the iterators returned in that code, or sort the contents of a non-const container in-place.
If all you want is a C#/Python style input iterator, you can probably simplify production with CRTP as Cort Ammon suggested in comments, and you can definitely lose the non-const overloads.
A summary of subsequent comments:
Q: In PogoStickLang, I can create a new pogo stick by just typing
!. In RocketLang I need to type loads more stuff to make a pogo stick, and it seems over-complex
A: In RocketLang you created a rocket, which is why it's more complex than a pogo stick. Either
- create a pogo stick if that's all you want. It's more work when not using a language tightly focused on the unique needs of pogo stick-users, but still less work than creating a rocket
- or, create a rocket and actually take advantage of all the things it can do that a pogo stick can't, like flying to the moon or incinerating your enemies.
No, this isn't entirely serious. No, I'm not suggesting that C# is a toy language, and no, I'm not suggesting this is a proportionate comparison of the relative powers of Input- and RandomAccessIterators. I don't have anything against pogo sticks and rarely, if ever, incinerate my enemies.