Why is this call to swap() ambiguous?

被刻印的时光 ゝ 提交于 2019-12-03 10:39:37

Because std::allocator<T> is used as a template type argument, the std namespace is an associated namespace for ADL.

[basic.lookup.argdep]/2, bullet 2, emphasis mine:

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.

...and pointers have the same set of associated namespaces/classes as the type they point to:

If T is a pointer to U or an array of U, its associated namespaces and classes are those associated with U.

The set of associated namespaces is determined based on various types visible from the argument types. Notably, for class templates the associated namespaces include the associated namespaces of all template arguments. When looking up unqualified functions using argument dependent look-up all associated namespaces are searched.

The template argument list of foo<int> is actually foo<int, std::allocator<int>>, thereby dragging namespace std into the picture and there is already a general overload for swap() available from there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!