gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:49 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com