Search code examples
c++genericsc++11iterator

C++11 iterator interface


Is it possible to create in C++11 a function, which will accept any iterator as input argument in particular stl containers like vector or list?

I want to write something like

void f(iterator<int> i){
   for (auto el : i)
      cout << el;
}
int main(){
   vector<int> v;
   list<int> l;
   ...
   f(v);
   f(l);
}

Is it possible?


Solution

  • No. You can write a function template that accepts any iterator as parameter, but not a function.

    If you had a class that did type erasure for iterators then you could take that as a parameter. Here's an article and code for iterator type erasure.