Why the compiler complains that it can not find a match?

N

Note Myself

Today I wanted to solve an problem: I need to find out in the code if the
iterator passed is a st::vector<T>::iterator, or
std::vector<T>::reverse_iterator. I spent a lot of time tweaking a template
function which is suppose to sovle the issue -

template<typename T>
bool IsForwardIterator(typename T::iterator)
{ return true; }

template<typename T>
bool IsForwardIterator(typename T::reverse_iterator)
{ return false; }

vector<int> vec;

--> bool bFlag = IsForwardIterator(vec.begin());
--> bFlag = IsForwardIterator(vec.rbegin());

The compiler (VC 7.1) complains that it can not locate the overloaded
function.

What might be wrong. BTW, I solve the problem using typeid function. But it
is irrelevant now.
 
O

Old Wolf

Today I wanted to solve an problem: I need to find out in the code if the
iterator passed is a st::vector<T>::iterator, or
std::vector<T>::reverse_iterator. I spent a lot of time tweaking a template
function which is suppose to sovle the issue -

template<typename T>
bool IsForwardIterator(typename T::iterator)
{ return true; }

template<typename T>
bool IsForwardIterator(typename T::reverse_iterator)
{ return false; }

vector<int> vec;

--> bool bFlag = IsForwardIterator(vec.begin());
--> bFlag = IsForwardIterator(vec.rbegin());

The compiler (VC 7.1) complains that it can not locate the overloaded
function.

The compiler can't deduce 'T' in this example. To do so, it
would have to go through every possible class it knows about
and see if any of them has a typedef or member class called
'iterator', and then, see if that value matches what you've
passed on.
 
V

Victor Bazarov

Note said:
Today I wanted to solve an problem: I need to find out in the code if
the iterator passed is a st::vector<T>::iterator, or
std::vector<T>::reverse_iterator. I spent a lot of time tweaking a
template function which is suppose to sovle the issue -

template<typename T>
bool IsForwardIterator(typename T::iterator)
{ return true; }

template<typename T>
bool IsForwardIterator(typename T::reverse_iterator)
{ return false; }

vector<int> vec;

--> bool bFlag = IsForwardIterator(vec.begin());
--> bFlag = IsForwardIterator(vec.rbegin());

The compiler (VC 7.1) complains that it can not locate the overloaded
function.

What might be wrong.

A member type (or a typedef) is not a deducible context for the template.
The compiler is not required to figure out that 'T' is 'vector<int>' if
you supply to it 'vector said:
BTW, I solve the problem using typeid function.
But it is irrelevant now.

It may not be good enough. Read about type traits. The header
<iterator> contains 'std::iterator_traits' template which can serve
you with the necessary information.

BTW, both 'iterator' and 'reverse_iterator' are Forward iterators
(for a vector they are actually of RandomAccess kind). I think you
may be misunderstanding the meaning of the term "forward iterator".

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top