c++ - 什么时候考虑对不合格的从属名称进行ADL查找?

标签 c++ c++17

我在cpp引用中对此example感到困惑。
我认为普通+ ADL查找在两种情况下都会产生以下集合:f(char)(普通查找),
f(int)/ f(E)(ADL查找,因为它考虑了来自POI的可见性)。 然后重载解析将在第一种情况下选择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在与参数类型关联的 namespace (或类)中查找功能。因此,如果您在命名空间enum E中声明X,则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
}

关于c++ - 什么时候考虑对不合格的从属名称进行ADL查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63392144/

相关文章:

c++ - C++ 中具有多态性的静态方法

c++ - 模板模板参数或模板参数

c++ - 添加图标时 CMFCStatusBar 更改高度

c++ - C++比较运算符重载

c++ - std::launder 的目的是什么?

c++ - constexpr if 和 static_assert

c++ - 如何在自定义类中使用 Qt 属性?

c++ - 传递 std::ref(value) 时是否应应用 std::optional 的推导指南?

c++ - 使用STL的简单 View

c++ - 使用带有 linux 的 std::filesystem 是坏的还是文件系统树无法访问?