为什么调用swap()会产生歧义?

23
以下是程序内容:
#include <algorithm>
#include <utility>
#include <memory>

namespace my_namespace
{


template<class T>
void swap(T& a, T& b)
{
  T tmp = std::move(a);
  a = std::move(b);
  b = std::move(tmp);
}

template<class T, class Alloc = std::allocator<T>>
class foo {};

}

int main()
{
  my_namespace::foo<int> *a, *b;

  using my_namespace::swap;

  swap(a,b);

  return 0;
}

以下命令在我的系统上会导致g++clang都出现编译错误:

$ clang -std=c++11 swap_repro.cpp -I.
swap_repro.cpp:28:3: error: call to 'swap' is ambiguous
  swap(a,b);
  ^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/../../../../include/c++/5.2.1/bits/algorithmfwd.h:571:5: note: candidate function [with _Tp = my_namespace::foo<int, std::allocator<int> > *]
    swap(_Tp&, _Tp&)
    ^
swap_repro.cpp:10:6: note: candidate function [with T = my_namespace::foo<int, std::allocator<int> > *]
void swap(T& a, T& b)
     ^
1 error generated.

$ g++ -std=c++11 swap_repro.cpp -I.
swap_repro.cpp: In function ‘int main()’:
swap_repro.cpp:28:11: error: call of overloaded ‘swap(my_namespace::foo<int>*&, my_namespace::foo<int>*&)’ is ambiguous
   swap(a,b);
           ^
swap_repro.cpp:28:11: note: candidates are:
swap_repro.cpp:10:6: note: void my_namespace::swap(T&, T&) [with T = my_namespace::foo<int>*]
 void swap(T& a, T& b)
      ^
In file included from /usr/include/c++/4.9/bits/stl_pair.h:59:0,
                 from /usr/include/c++/4.9/utility:70,
                 from /usr/include/c++/4.9/algorithm:60,
                 from swap_repro.cpp:1:
/usr/include/c++/4.9/bits/move.h:166:5: note: void std::swap(_Tp&, _Tp&) [with _Tp = my_namespace::foo<int>*]
     swap(_Tp& __a, _Tp& __b)
     ^

我不明白为什么std::swap被视为候选项重载,但这与foo使用std::allocator<T>有关。

消除foo的第二个模板参数使程序可以编译而不出错。

2个回答

19
因为std::allocator<T>被用作模板类型参数,所以ADL的关联命名空间包括std命名空间。 [basic.lookup.argdep]/2,第二条,重点是:

此外,如果T是类模板特化,则其相关的命名空间和类还包括:提供给模板类型参数的模板参数类型所关联的命名空间和类(不包括模板模板参数);任何模板模板参数是成员的命名空间;任何用作模板模板参数的成员模板的类。

...指针与它们所指向的类型具有相同的相关命名空间/类集合:

如果T是指向UU数组的指针,则其相关的命名空间和类是与U相关的。


11

基于参数类型可见的各种类型,确定相关联的命名空间集。值得注意的是,对于类模板,相关联的命名空间包括所有模板参数的关联命名空间。在使用参数依赖查找查找不限定函数时,将搜索所有相关的命名空间。

foo<int> 的模板参数列表实际上是 foo<int, std::allocator<int>>,因此将命名空间std引入到图片中,并且已经从那里提供了一个通用的swap()重载。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接