c++ - 如何在仍然允许 ADL 等的情况下调用同名的其他函数

标签 c++

如何在第二个 f() 中进行调用,通过非限定查找找到第一个 f(),以便 ADL 和普通名称查找规则仍然有效?

注意:我不想更改任何函数的名称。

namespace x 
{
    void f()
    {
    }

    class C 
    {
        void f() 
        {
            x::f();
        }
    };
}

最佳答案

只需引入函数:

void f()
{
    using x::f;
    f(); // calls x::f()
}

这样做的原因是,复制 Mike Seymour's answer :

Because the using declaration brings x::f into the scope of f, which is narrower than that of C. Unqualified lookup considers the local block scope, finds a match, and stops before considering the wider class scope. There is no argument-dependent lookup since there are no function arguments, so no further scopes are considered.

关于c++ - 如何在仍然允许 ADL 等的情况下调用同名的其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315952/

相关文章:

c++ - 未调用 ReadFileEx() 完成例程

c++ - 使用SIMD指令的平行二项式系数

c++ - 子类按钮不会在每次重复单击时生成动画

c++ - 在 C++ 中将 unsigned int 转换为 int 时避免整数溢出的有效方法是什么?

C++ 类构造和成员初始化

c++ - 可变参数模板的多重继承 : how to call function for each base class?

c++ - 如果值等于键,则合并映射中的条目

c++ - 结构成员初始化

c++ - OpenCV 3.0 - 如何从 CSV 字符串创建 cv::Mat?

c++ - DirectX 不正确的纹理