Type-less iteration in C++
Say that you have declared a variable like this:
because the return-type of foo is obscured thanks to heavily use of templates.
However, this type is “container-like” and you would very much like to retrieve individual items from this container through an iterator. As is convention, there is a typedef inside the type of v which defines the type of such an iterator, but how do we get to this iterator, if we never specified the type of v, but just glossed over it with auto?
Fear not, dear reader! Together with auto is a counterpart called decltype, which can be used to get the type of a variable. Since v was even a reference, we need some extra template magic to unpack it further:
Now we are free to use this newly-found type in our iterations:
But what if this v variable is “matrix-like” and have another level of iterators inside each of the iterators again? Well, now that we are already inside the rabbit hole, we can just continue to dig deeper: