c++ - ADL 不考虑与模板参数关联的 namespace 吗?

标签 c++ argument-dependent-lookup

考虑简单的代码:

template<int N> struct foo{};

namespace N
{
    const int a=1;
    void bar(foo<1>& x){}
}

int main()
{
    bar(foo<N::a>());
    return 0;
}

代码不应该工作吗?我想知道为什么它不起作用。谢谢

最佳答案

[basic.lookup.argdep]/2:

[ Note: Non-type template arguments do not contribute to the set of associated namespaces.—end note ]

对于注释中链接的代码,命名空间中的 typedef 也不够,但出于其他原因。查找基于已解析的类型,而不是基于包含 typedef 本身的命名空间(或者,等效地,using)。

例如,如果您有如下代码:

namespace A { 
    class T {};
}

namespace B { 
    typedef A::T TT;
}

使用 B::TT 作为参数会将 namespace A 添加到查找中,但不会将 namespace B 添加到查找中。

关于c++ - ADL 不考虑与模板参数关联的 namespace 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33972793/

相关文章:

c++ - 我正在寻找可以在图形编辑程序中进行的任何优化

c++ - 我需要对 Stroustrup 关于 ADL 的新书上的这个例子做一些澄清

C++ Koenig(参数相关)查找 : What if two namespaces functions in different namespaces have the same argument types?

c++ - ADL 和 friend 注入(inject)

c++ - 这个模板类型推导和重载解析是如何工作的?

c++ - 如何根据我的操作系统包含不同的 header ?

c++ - 从文件中读取并比较/合并数据以创建新文件

C++ Builder 如何用 vector 构建动态对象?

c++ - Docker容器中的可执行文件不会从gdb远程调试中注册断点

c++ - 为什么 adl 更喜欢 'boost::range_detail::operator|' 而不是本地 'operator|'?