在 Visual C++ 上使用模板函数时遇到问题
have trouble using template function on visual c++
我最近正在阅读 C++Primer 上的模板部分,我想在我的 VS2013 上尝试一下。
我写了一个模板查找如下。
#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);
}
但是visual studio告诉我
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
我很困惑,我没有使用 "using namespace std",谁能告诉我为什么 "find" 的 std 版本会出现在这里?
非常感谢你的帮助:D.
在 vector
中包含头文件 algorithm
。
由于 ADL
在命名空间 std
中查找将被使用,因为 vector::const_iterator
在 std
命名空间中。如果手动包含 algorithm
头文件,gcc
和 clang
也是如此。
我最近正在阅读 C++Primer 上的模板部分,我想在我的 VS2013 上尝试一下。 我写了一个模板查找如下。
#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);
}
但是visual studio告诉我
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
我很困惑,我没有使用 "using namespace std",谁能告诉我为什么 "find" 的 std 版本会出现在这里?
非常感谢你的帮助:D.
在 vector
中包含头文件 algorithm
。
由于 ADL
在命名空间 std
中查找将被使用,因为 vector::const_iterator
在 std
命名空间中。如果手动包含 algorithm
头文件,gcc
和 clang
也是如此。