c++ 命名空间解析(基于参数的“自动使用”?)

Jul*_*n-L 4 c++ namespaces token-name-resolution

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

namespace ns1 {
    void myfunc();
}

myfunc();        // compiler complains
ns1::myfunc();   // good
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

Bal*_*Pal 5

Q1:“适当的”是有争议的,但基本上是的,它选择了结合各种“关联”命名空间的函数,这对于许多样本都可以正常工作。

Q2:您让几十人加入 WG21,并最终将“修复 ADL”提案中的一项投票变为现实,到目前为止,这些尝试都失败了。

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