繁体   English   中英

何时考虑对不合格的从属名称进行 ADL 查找?

[英]When is ADL lookup is considered for unqualified dependent name?

我在 cpp 参考中遇到了这个例子

我认为普通 + ADL 查找在这两种情况下都会产生以下集合:f(char)(普通查找)、f(int)/f(E)(ADL 查找,因为它考虑了 POI 的可见性)。 然后重载决议将 select f(E) 在第一种情况下和 f(int) 在另一种情况下。

您能否向我解释一下在这种情况下到底发生了什么(查找、重载解决方案)?

非常感谢!

示例:

void f(char); // first declaration of f
 
template<class T> 
void g(T t) {
    f(1);    // non-dependent name: lookup finds ::f(char) and binds it now
    f(T(1)); // dependent name: lookup postponed
    f(t);    // dependent name: lookup postponed
//  dd++;    // non-dependent name: lookup finds no declaration
}
 
enum E { e };
void f(E);   // second declaration of f
void f(int); // third declaration of f
double dd;
 
void h() {
    g(e);  // instantiates g<E>, at which point
           // the second and the third uses of the name 'f'
           // are looked up and find ::f(char) (by lookup) and ::f(E) (by ADL)
           // then overload resolution chooses ::f(E).
           // This calls f(char), then f(E) twice
    g(32); // instantiates g<int>, at which point
           // the second and the third uses of the name 'f'
           // are looked up and find ::f(char) only
           // then overload resolution chooses ::f(char)
           // This calls f(char) three times
}

ADL 在与参数类型关联的命名空间(或类)中查找 function。 因此,如果您在命名空间X中声明enum E ,ADL 只会在命名空间X内查找(见下文)。基本类型为int没有任何关联的命名空间。 所以基本类型的 ADL 永远找不到任何东西。

void f(char); // first declaration of f
 
template<class T> 
void g(T t) {
    f(1);    // non-dependent name: lookup finds ::f(char) and binds it now
    f(T(1)); // dependent name: lookup postponed
    f(t);    // dependent name: lookup postponed
//  dd++;    // non-dependent name: lookup finds no declaration
}

namespace X {
    enum E { e };
    void f(E);   // second declaration of f
    void f(int); // third declaration of f
    }
double dd;

void f(int);//the global namespace is not associated to fundamental types
 
void h() {
    //The associated scope of X::e is namespace X.
    g(X::e);  // instantiates g<E>, at which point
           // the second and the third uses of the name 'f'
           // are looked up and find ::f(char) (by lookup) and X::f(int) and X::f(E) (by ADL)
           // then overload resolution chooses X::f(E).
           // This calls f(char), then X::f(E) twice

    //Fundamental types do not have any associated namespace
    g(32); // instantiates g<int>, at which point
           // the second and the third uses of the name 'f'
           // are looked up and find ::f(char) only
           // then overload resolution chooses ::f(char)
           // This calls f(char) three times
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM