为什么对 swap() 的调用不明确?
Why is this call to swap() ambiguous?
下面的程序
#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
的第二个模板参数可以使程序编译无误。
因为 std::allocator<T>
用作模板类型参数,所以 std
命名空间是 ADL 的关联命名空间。
[basic.lookup.argdep]/2,项目符号 2,强调我的:
Furthermore, if T
is a class template specialization, its associated
namespaces and classes also include: the namespaces and classes
associated with the types of the template arguments provided for
template type parameters (excluding template template parameters);
the namespaces of which any template template arguments are members;
and the classes of which any member templates used as template
template arguments are members.
...并且指针与它们指向的类型具有相同的一组关联 namespaces/classes:
If T
is a pointer to U
or an array of U
, its associated namespaces and
classes are those associated with U
.
关联的命名空间集是根据参数类型中可见的各种类型确定的。值得注意的是,对于 class 模板,关联的命名空间包括所有模板参数的关联命名空间。使用参数相关查找查找不合格的函数时,将搜索所有关联的命名空间。
foo<int>
的模板参数列表实际上是 foo<int, std::allocator<int>>
,因此将命名空间 std
拖入图片中,那里已经有 swap()
的通用重载可用.
下面的程序
#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
的第二个模板参数可以使程序编译无误。
因为 std::allocator<T>
用作模板类型参数,所以 std
命名空间是 ADL 的关联命名空间。
[basic.lookup.argdep]/2,项目符号 2,强调我的:
Furthermore, if
T
is a class template specialization, its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provided for template type parameters (excluding template template parameters); the namespaces of which any template template arguments are members; and the classes of which any member templates used as template template arguments are members.
...并且指针与它们指向的类型具有相同的一组关联 namespaces/classes:
If
T
is a pointer toU
or an array ofU
, its associated namespaces and classes are those associated withU
.
关联的命名空间集是根据参数类型中可见的各种类型确定的。值得注意的是,对于 class 模板,关联的命名空间包括所有模板参数的关联命名空间。使用参数相关查找查找不合格的函数时,将搜索所有关联的命名空间。
foo<int>
的模板参数列表实际上是 foo<int, std::allocator<int>>
,因此将命名空间 std
拖入图片中,那里已经有 swap()
的通用重载可用.