1v0
1v0

Reputation: 352

C++11: Storing an Iterator without knowing its container

Is there, in C++11, something along the line of an object of type Iterator<T> which just stores an iterator without knowing what std container the iterator stems from, only the type of data the container stores?

If such an object does not exist, what is the reason for it's absence?

Upvotes: 3

Views: 184

Answers (2)

yaqwsx
yaqwsx

Reputation: 569

There is no base class for iterators in C++, since they are designed for high performance (so no virtual calls are welcomed).

All iterators types are resolved in compile time and all "iterator storages" types also have to be resolved in compile time.

What is your use case? There might be another, better solution.

Upvotes: 3

rubenvb
rubenvb

Reputation: 76785

No, there is no common iterator base class and type erasure workarounds (if even possible in this case) would only defer the need to store the type somewhere else somehow.

Maybe you should rethink what it is you are trying to achieve and ask a question about that?

Upvotes: 0

Related Questions