C++ 命名空间解析("automatic using"基于参数?
c++ namespace resolution ("automatic using" based on arguments?)
从名称空间外部调用在名称空间中声明的函数时,通常需要显式地使用名称空间前缀:
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;
}
Q2:如何停用此行为?
编辑:编译器:gcc version 4.7.2 (Debian 4.7.2-5)
Q1: '适当'是有争议的,但本质上是的,它选择了结合各种"相关"名称空间的函数,这对于许多示例都很好。
Q2:你让几十个人加入WG21,并最终投票支持"修复ADL"的提案之一,到目前为止,这些尝试都失败了。
Q2,实用的答案:ADL仅适用于非限定查找,因此您始终可以选择使用限定名称。(前缀::std::等等…)。或者完全停止使用名称空间。除此之外,你必须遵守"柯尼格测不准原理"
相关文章:
- 类成员函数参数列表是否可以依赖于模板参数?
- 专用于可变参数模板成员函数
- C++ 使函数调用依赖于模板参数
- 如何修复"没有依赖于模板参数的参数'glGenVertexArrays'......"C++ 中的错误
- 将 std::bind 应用于带有参数 <boost::asio::ip::tcp::socket> 时出错?
- 保证复制 elis 是否适用于函数参数?
- 部分专用于可变参数模板类的方法
- 将 const 限定符应用于模板参数时的不同编译器行为
- 如何定义依赖于模板参数的类型定义
- 使用 std::应用于可变参数包
- 类型依赖于可变参数模板的类
- 如何将函数应用于可变参数列表并将它们猫到元组
- 如何将函数应用于可变参数列表的每个组件并返回可变参数列表
- C++ 错误:"没有依赖于模板参数的'setw'参数
- 依赖于模板参数的范围查找
- 如何调用使用数组将函数应用于可变参数包的成语
- 链接式"运算符>>"相对于可变参数模板函数的优势?
- 使用的类型别名如何指定依赖于模板参数的模板模板参数
- 类模板,独立于其参数的排列
- 如何设置依赖于其他参数的参数默认值