can't pass an iterator to a function template

I want to write a function template that accepts an iterator as parameter.

The included headers and data are:

1
2
3
4
5
6
7
8
9
10
#include <algorithm>        /// find()
#include <vector>
#include <list>
#include <iostream>

using namespace std;


vector<int> v {1, 2, 3, 4, 5, 6};
list<int> l {1, 2, 3, 4, 5, 6};



The function template should print a message about whether the value is found or not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template<typename C>
void msg(C& c,
         C::iterator p,
         int i)
{
    if (p == c.end())
    {
        cout << i << " not found";
    }
    else
    {
        cout << i << " found at position # "
             << distance(c.begin(), next(p));
    }

    cout << endl;
}


However, I get the following compilation error:


error: 'C::iterator' is not a type|



The tester is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void test()
{
    auto pv4 = find(v.begin(), v.end(), 4);
    auto pv9 = find(v.begin(), v.end(), 9);

    auto pl4 = find(l.begin(), l.end(), 4);
    auto pl9 = find(l.begin(), l.end(), 9);

    msg(v, pv4, 4);
    msg(v, pv9, 9);

    msg(l, pl4, 4);
    msg(l, pl9, 9);
}



So, how do I pass an iterator to a function template?

Thanks.
> error: 'C::iterator' is not a type

Disambiguate with the typename keyword.

1
2
template < typename C >
void msg( const C& c, typename C::iterator p, const typename C::value_type& v ) ;


See: 'The typename disambiguator for dependent names'
http://en.cppreference.com/w/cpp/language/dependent_name
Topic archived. No new replies allowed.