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

Discussion in 'C++' started by Note Myself, Apr 4, 2007.

  1. Note Myself

    Note Myself Guest

    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.
     
    Note Myself, Apr 4, 2007
    #1
    1. Advertising

  2. Note Myself

    Old Wolf Guest

    On Apr 4, 2:17 pm, "Note Myself" <> wrote:
    > 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.
     
    Old Wolf, Apr 4, 2007
    #2
    1. Advertising

  3. Note Myself wrote:
    > 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<int>::iterator'.

    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 4, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2lycGVsaWRvcg==?=

    debugger complains: auto-attach to process 'aspnet_wp.exe'

    =?Utf-8?B?c2lycGVsaWRvcg==?=, Aug 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    2,161
    =?Utf-8?B?c2lycGVsaWRvcg==?=
    Aug 29, 2005
  2. Old Wolf
    Replies:
    4
    Views:
    345
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    2
    Views:
    283
    Adrian
    Aug 9, 2005
  5. filmil
    Replies:
    4
    Views:
    485
    filmil
    May 8, 2007
Loading...

Share This Page