c++ 命名空间分辨率 ("automatic using"基于参数?)

标签 c++ namespaces token-name-resolution

当我从这个命名空间之外调用在这个命名空间中声明的函数时,我通常需要显式地加上这个命名空间的前缀:

namespace ns1 {
    void myfunc();
}

myfunc();        // compiler complains
ns1::myfunc();   // good

但是我有这种情况,编译器似乎可以自动设计我想使用的函数:

namespace mymath {
    struct Vector3 {
        float x, y, z;
    };
    Vector3 vector_cross(const Vector3& lhs, const Vector3 &rhs)
    {
        return lhs; // Pretend this is a cross product
    }
} // ns

int main(int argc, char** argv)
{
    mymath::Vector3 v, w, x;
    x = vector_cross(v, w);   // <---- Here, I do not need to
                              //       prefix with the namespace
    return 0;
}

Q1:是不是因为编译器可以根据参数类型自动选择合适的函数?还是别的?


经过更多测试,我发现如果我在另一个命名空间中添加另一个具有相同声明的函数,我的编译器不会报错:

namespace mymath {
    // same as above
} // ns

namespace math2 {
    mymath::Vector3 vector_cross(const mymath::Vector3& lhs, const mymath::Vector3 &rhs)
    {
        return rhs; // Return rhs this time
    }
} // ns

int main(int argc, char** argv)
{
    mymath::Vector3 v, w, x;
    x = vector_cross(v, w);   // <---- Here, which one did the compiler chose?
    return 0;
}

问题 2:如何取消激活此行为?

编辑: 我的编译器:gcc 版本 4.7.2 (Debian 4.7.2-5)

最佳答案

Q1:'appropriate' 是有争议的,但本质上是的,它选择结合各种“关联”命名空间的函数,这对许多样本都很好。

Q2:你让几十个人加入 WG21,并最终投票通过了一项“修复 ADL”提案,到目前为止,尝试都失败了。

Q2,务实的回答:ADL 仅适用于非限定查找,因此您始终可以选择使用限定名称。 (前缀::std::等等......)。或者完全停止使用 namespace 。除此之外,您还受制于“Koenig 不确定性原理”。

关于c++ 命名空间分辨率 ("automatic using"基于参数?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17114927/

相关文章:

C++ `using` 模板类中类型别名的命令

c++ - 在 Selene 中包装一个枚举以便在 Lua 中访问

c# - 如何在不影响其他项目的情况下重构C#中的命名空间?

c++ - namespace 检测

c++ - C++ 使用静态名称解析还是动态名称解析?

c++ - 类模板中名称解析的实际结果与c++ 03标准不同

c++ - 这个地址

c++ - 在 C++ 编译时是否没有内置方法来计算功率?

xml - 使用 SSIS 加载 XML 对 SQL 具有多个命名空间

actionscript-3 - 如何显式引用全局包?