Examples https://stackoverflow.com/a/5076640/321013 such as https://groups.google.com/forum/#!original/comp.lang.c++.moderated/1Qz71aFnXlI/Gd71gwRaWssJ启用输出std
类型解释如何ADL http://en.wikipedia.org/wiki/Argument-dependent_name_lookup可用于“注入”某个函数/运算符,具体取决于 fn/op 所应用的类型。
我想知道 ADL full 是否适用于全局命名空间,即是否声明了类型(或通过using
) at 全局命名空间范围 https://stackoverflow.com/a/10269085/321013让 ADL 在全局命名空间中寻找匹配的函数?
具体来说,这些是等价的。日常活动能力?:
// 1 - at global namespace scope
struct GlobalType {};
template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, GlobalType const& x)
{
os << ...;
return os;
}
// 2 - within namespace
namespace ecaps {
struct EcapsType {};
template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, EcapsType const& x)
{
os << ...;
return os;
}
}
// 3 - Type brought to global NS via using, function at global scope
namespace other {
struct OtherType {};
}
using other::OtherType;
template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, OtherType const& x)
{
os << ...;
return os;
}
沃特。不需要 ADL 的全局命名空间范围:(在现已删除的答案后更新)
One 委员会名人 Daniel Krügler 描述了 ADL 问题 https://groups.google.com/forum/#!original/comp.lang.c++.moderated/1Qz71aFnXlI/Gd71gwRaWssJ像这样:
此非限定调用具有非限定名称查找的效果
发生这种情况,因此编译器会搜索
姓名operator<<
。开始从词汇位置operator<<
发现呼叫“向上”(...) 从当前命名空间开始并且所有
包含该命名空间的命名空间(包括全局
命名空间,顺便说一句。)和 - ...
强调。矿。请注意外部名称空间如何被描述为仅被视为“...从词汇位置...“。 他继续:
...并且 - 作为第二条路线 - 它执行此的第二阶段
编译器在所谓的关联命名空间中进行查找
此调用中出现的参数类型。
在所提供的示例中,搜索的第一阶段失败,因为
在这一点上#include <iterator>
存在,不存在
相应的operator<<
对于任何命名空间中的这些参数类型。
请注意,您的声明operator<<
按词汇提供after的调用点operator<<
发生在某些地方
库头。第二阶段的搜寻工作还将
考虑地点follow实际的函数调用,但仅限于关联的命名空间内。
大胆强调。矿。所以在我看来is与 ADL 适用于全局命名空间相关。当然,我很容易误解一些东西。
注:这个may是标准的一种情况,只是没有以某种方式明确提及它,因为全局 NS 就像任何其他命名空间一样——但也可能不是,我对标准的了解非常有限。