jankjn
jankjn

Reputation: 5

have trouble using template function on visual c++

I'm reading the template section on C++Primer recently, and i want to try it on my VS2013. I write a template find as following.

#include <vector>

template <typename iteratorT, typename valT>
iteratorT find(const iteratorT &up, const iteratorT &end, const valT &val)
{
    auto iter = up;
    while (iter != end && *iter != val)
        ++iter;
    return ++iter;
}

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    auto i = find(v.cbegin(), v.cend(), 7);
}

but visual studio tell me

    1   IntelliSense: more than one instance of function template "find" matches the argument list:
        function template "_InIt std::find(_InIt _First, _InIt _Last, const _Ty &_Val)"
        function template "iteratorT find(const iteratorT &up, const iteratorT &end, const valT &val)"
        argument types are: (std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<int>>>, std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<int>>>, int)   d:\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp  16  11  ConsoleApplication1

I'm confused, I didn't use "using namespace std", can anyone tell me why will the std version of "find" come here ?

I'll be grateful for your help :D.

Upvotes: 0

Views: 63

Answers (1)

ForEveR
ForEveR

Reputation: 55887

In vector header file algorithm is included. Due ADL find in namespace std will be used since vector::const_iterator is in std namespace. Same will be with gcc and clang, if algorithm header file is included manually.

Upvotes: 1

Related Questions