考虑以下代码:
#include <vector>
#include <algorithm>
template <typename Input1, typename Input2, typename Output>
void merge(Input1 begin1, Input1 end1, Input2 begin2, Input2 end2, Output out)
{
}
int main()
{
std::vector<int> a = {1, 2};
int b[] = {3, 4};
int c[4];
merge(a.begin(), a.end(), b, b + 2, c);
}
编译产量:
$ clang++ -std=c++11 -stdlib=libc++ merge.cpp
merge.cpp:15:5: error: call to 'merge' is ambiguous
merge(a.begin(), a.end(), b, b + 2, c);
^~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:4056:1: note:
candidate function [with _InputIterator1 = std::__1::__wrap_iter<int *>,
_InputIterator2 = int *, _OutputIterator = int *]
merge(_InputIterator1 __first1, _InputIterator1 __last1,
^
merge.cpp:5:6: note: candidate function [with Input1 = std::__1::__wrap_iter<int
*>, Input2 = int *, Output = int *]
void merge(Input1 begin1, Input1 end1, Input2 begin2, Input2 end2, Output out)
^
1 error generated.
编译器版本:
$ clang++ --version
Apple LLVM version 5.0 (clang-500.2.78) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
为什么叫merge
模棱两可?不确定我的意思是::merge()
还是std::merge()
,虽然很清楚(?)它应该是::merge()
因为我没有指定任何using
指令。我的merge
函数在全局命名空间中,我认为它不会与std
命名空间中的任何内容冲突(因为这是命名空间的要点,对吧?)。如果我更改a
为int
像其他数组一样的数组,它的编译不会有任何歧义。此外,添加冒号和调用也::merge()
可以正常工作。
所以我的问题是:这是 Clang 中的错误,还是我对命名空间有误解?merge()
当两个函数不在同一个命名空间中并且我没有使用任何指令使其std::merge()
可见时,为什么我的调用会导致歧义?using