c++ - c ++命名空间的奇怪行为
问题描述
这是一些代码,显示了奇怪之处:
namespace ns
{
typedef int int_type;
class classus {
public:
int a = 0;
};
void print(classus c){
printf("a equals %i \n", c.a);
}
void print(int_type a){
printf("a equals %i \n", a);
}
}
int main(int argc, char* argv[])
{
ns::int_type a1 = 2;
ns::classus a2;
ns::print(a1); // this line wont work without explicit ns:: scope
print(a2); // this line works with or without explicit ns:: scope
}
它在 Visual Studio 2017 上构建和运行。Intellisense 也非常满意。
似乎包含在命名空间中的类变量会污染该命名空间范围的整行。这是错误还是功能?无论哪种方式,是否有一些关于此的文档...没有找到任何
解决方案
让您感到困惑的功能称为 ADL(参数相关查找)
依赖于参数的查找,也称为 ADL 或 Koenig 查找,是用于在函数调用表达式中查找非限定函数名称的一组规则,包括对重载运算符的隐式函数调用。除了通常的非限定名称查找所考虑的范围和名称空间之外,还会在其参数的名称空间中查找这些函数名称。
依赖于参数的查找使使用在不同命名空间中定义的运算符成为可能。
在您的示例a2
中,来自命名空间ns
足以让编译器ns
在查找print
.
您的示例中有趣的部分是它int_type
也来自ns
,尽管它只是一个 typedef 并且int
没有在 中声明ns
。考虑 typedef 不会引入新类型(而是别名)。所以a2
真的是一个int
。
PS:这不是特定于 Visual Studio。任何符合标准的编译器都应该接受发布的代码。
推荐阅读
- javascript - 在现有的 React 项目中实现 babel/typescript
- mysql - ionic 和 mongodb 是必须的,还是我可以使用 mysql
- python-3.x - Django Admin 中的自定义 ModelForm
- laravel - JWT 认证总是返回 401 未授权
- javascript - 如何检查哪个选项卡处于活动状态?
- r - 在r中的一页中组合几个ggplot
- java - 围绕原点旋转“细胞”
- node.js - 无法读取未定义的属性“活动”
- java - 如何像在JavaFX中通过的信号一样为线条着色
- web-services - 如何在 OpenEdge Progress4GL 中将 XML 响应转换为临时表?