C++: Generic, cross container forward iterator?

Started by
5 comments, last by the_edd 13 years, 6 months ago
I'm trying to find a generic, cross container, forward iterator such that I can write code that resembles:

void function f(iterator i){    ...}f(some_std_vector.begin());f(some_std_list.begin());


Where some_vector and some_list will always be containers storing the same type. I don't anticipate the need for associative container (std::map) compatibility.

Pretty vague, but hopefully you guys get the idea. The obvious solution is to use templates, and I would, but doing so would require me to modify someone elses code, and that could quickly become a much bigger pain in the butt.

Any ideas?
Advertisement
I can't think of a solution offhand that wouldn't require modifying the other code...

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
I can't think of a solution offhand that wouldn't require modifying the other code...


The best I can think of at the moment is to write a method for each container type, which loads the data into some standard container, which then gets passed into another method which does all the work. This seems like overkill, and a tad inefficient. The other option to just pick a container and live with it, and another is to write a whole heap of duplicate code.

I was sort of hoping there's some standard, quicker way of doing this that doesn't require me to write a heap of redundant code, or lock into one container. Things aren't looking up, huh?

[Edited by - Liam M on October 12, 2010 7:02:07 AM]
Can you explain exactly how you would need to modify the other code? Why can't you just do:

template <typename IteratorT>void foo(IteratorT begin, IteratorT end){   for(IteratorT iter = begin; iter != end; ++iter)      iter->Frobnicate();}


This sort of thing is done all over the standard library and works fine...

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
Can you explain exactly how you would need to modify the other code? Why can't you just do:


In a nutshell, because foo is a pure virtual method. The problem with modifying the code is that the library I'm modifying is built to be very compatible, and largely resemble, Nokia Qt. If I need to modify code external to my own, I need to go through various layers of management, and they won't like the necessary changes because it makes their product slightly less 'Qt like'. Additionally, if I were to modify the class such that the method was no longer virtual, that would also require me to involve management because it's non-standard behavior.

Quote:Original post by ApochPiQ
This sort of thing is done all over the standard library and works fine...


Oh don't get me wrong, my own code is riddled with similar code.
Alright, I've decided to write a few extra methods which shift data into a standard container, and make the overloaded methods virtual. This gives me a bit of leverage, in that I can provide reasonable functionality for people who really don't care about speed, as well as allowing people who do care about speed, to have each method call a template method such as the one shown above by ApochPiQ to take care of business.

If anyone has a better solution, I'd love to hear about it. For now, I think the above should be adequate.

Thanks ApochPiQ.
You're always going to need an "end" iterator, at least if you use the standard library's conventions.

However, I did once rustle up a runtime-polymorphic iterator. But it is slow (by nature). You're better off using a template, IMHO.

This topic is closed to new replies.

Advertisement