Home Receive iterators as arguments and know class
Reply: 0

Receive iterators as arguments and know class

user6106 Published in September 19, 2018, 3:19 am

I need to create a method that receives iterators as arguments and return a templated collection of the same type.

I created a minimal example the demonstrates my need:

#include <list>
#include <iostream>

using namespace std;

class A {
    int i;
    A(int _i) : i(_i) {}

    operator int() {
        return i;

class B {
    int i;
    B(int _i) : i(_i) {}

    operator int() {
        return i;

template <class T>
list<T> method(typename list<T>::iterator begin, typename list<T>::iterator end) {
    list<T> res; // I need the template class here

    for (; begin != end; begin++) {
        cout << *begin; // Whatever...

    return res;

int main() {
    list<A> listA = {1, 2, 3};
    list<B> listB = {4, 5, 6};

    auto res1 = method(listA.begin(), listA.end()); // Cannot change
    auto res2 = method(listB.begin(), listB.end()); // Cannot change

This is not a working example, but I am looking for a way to make this work.

The important part is the method and it's arguments, and that it will return a templated class with T. So I can change the method as much as i want but the auto res1 = method(listA.begin(), listA.end()); should stay the same. (not my code)

How can I do something like this?

share|improve this question
  • You could use this to deduce the type of dereferencing your iterator. – SirGuy Jan 10 at 18:34

4 Answers 4

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35465 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO