8 c++ namespaces compiler-errors ambiguous
以下代码无法编译.该错误似乎是对合并例程的某种模糊调用.我的理解是STL有一个在std命名空间中找到的合并例程,但据我所知,下面代码中的名称merge应该是唯一的.
如果我将merge重命名为xmerge,一切正常.问题是什么?冲突的名称来自何处?
#include <iostream>
#include <iterator>
#include <vector>
template<typename InputIterator1,
typename InputIterator2,
typename OutputIterator>
void merge(const InputIterator1 begin1, const InputIterator1 end1,
const InputIterator2 begin2, const InputIterator2 end2,
OutputIterator out)
{
InputIterator1 itr1 = begin1;
InputIterator2 itr2 = begin2;
while ((itr1 != end1) && (itr2 != end2))
{
if (*itr1 < *itr2)
*out = *itr1, ++itr1;
else
*out = *itr2, ++itr2;
++out;
}
while (itr1 != end1) *out++ = *itr1++;
while (itr2 != end2) *out++ = *itr2++;
}
int main()
{
std::vector<int> l1;
std::vector<int> l2;
std::vector<int> merged_list;
merge(l1.begin(),l1.end(),
l2.begin(),l2.end(),
std::back_inserter(merged_list));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Nav*_*een 17
编译器在你的merge函数和std::merge定义的函数之间变得混乱algorithm.使用::merge消除这种不确定性.此调用不明确,因为编译器使用Argument Dependendent Lookup在使用非限定函数名时搜索函数.