c++ - C++“使用命名空间”追溯可见性?
问题描述
我设置了一些代码来检测同名的方法或函数,并调用适当的方法或函数,或者abort
两者都不存在。但是,我注意到,如果调用此方法的编译单元通过 导入命名空间using
,则结果是无限递归而不是abort
. using
从前面的标题中可以看出,源文件中的声明似乎是追溯添加到全局命名空间中的,我觉得这很令人不安。
我已尽我所能在此处隔离问题以进行说明(使用 MSVC2017 编译):
// HEADER
namespace ns1
{
template<typename U>
inline auto foo_free(const U& u, int)
-> decltype(foo(u))
{
return foo(u);
}
template<typename U>
inline uint64_t foo_free(const U& u, char)
{
abort();
return 0;
}
template<typename U>
inline auto foo_method(const U& u, int)
-> decltype(u.foo())
{
return u.foo();
}
template<typename U>
inline uint64_t foo_method(const U& u, char)
{
return ns1::foo_free(u, 0);
}
}
namespace ns2
{
template<typename U>
inline uint64_t foo(const U& u)
{
return ns1::foo_method(u, 0);
}
}
// SOURCE
// removing this line results in expected behavior
using namespace ns2;
struct atype {};
bool detect_foo_confusion(void)
{
atype a;
const atype* cap = &a;
ns2::foo(cap); // problem
return true;
}
我能想到的唯一解决方案是命名ns2::foo
或其他foo
不同的东西,但这会对调用代码的外观产生一些不良后果。
- 这里发生了什么?
using namespace
总是以这种方式工作,还是这是模板的事情? - 有没有办法让我隔离
ns1
或ns2
保持相同的名字?
解决方案
推荐阅读
- pandas - 用另一个 DataFrame 的值替换 DataFrame 的值
- google-data-studio - Google Data Studio - 是否可以跨数据源进行过滤
- python - OpenCV 特征检测:如何按分数排名?
- excel - 通过 Excel VBA,打开 Word 文档以完成邮件合并。有没有办法自动设置 Word Doc Source 等于 Excel 电子表格?
- excel - 从单元格中删除所有图像,除了 vba 中的最后一个
- variables - ELProcessor - 如何将一个参数动态传递给 setValue() 方法?
- c++ - 输入字符时 cin 跳过
- macos - 如何以编程方式获取有关 MacOS 上活动 DNS 配置文件的信息?
- gremlin - AWS Neptune HTTP REST 将响应序列化程序从 GraphSON 更改为 JSON
- java - setBounds 不适用于 Java 中的 TextField