在使用内联名称空间中使用名称空间时,如何解决歧义
我使用内联命名空间进行版本控制,但有时会出现名称冲突,因为它将所有符号导出到其父范围中。有办法解决吗?当然,除了平坦的名称空间。删除inline
也不是一个选项,因为它破坏了源代码兼容性。
一个例子:
namespace ns {
inline namespace v1 {
namespace detail {
// implementation detail
}
class [[deprecated("use v2::some_class")]] some_class
{
// ...
};
} // namespace v1
namespace v2 {
namespace detail {
// implementation detail
}
class some_class
{
// ...
};
} // namespace v2
} // namespace ns
到目前为止,一切都很好。但是,当引入父命名空间中的另一个详细信息
时,所有这些都将陷入地狱:
namespace ns {
namespace detail {
class impl {};
} // namespace detail
} // namespace ns
void f()
{
ns::detail::impl i; // error: 'detail': ambiguous symbol
}
我想要content ns :: v1 ::详细信息
将合并到ns ::详细信息中
,不要与之冲突。
PS看起来仅在MSVC上发生。仅通过GCC和Clang检查,它们没有产生这种错误。在这种情况下,哪个编译器是正确的?
I use inline namespace for versioning, but sometimes name conflicts arise because it exports all symbols into its parent scope. Is there a way around this? Except flattening namespaces of course. Removing inline
is also not an option because it breaks source-code compatibility.
An example:
namespace ns {
inline namespace v1 {
namespace detail {
// implementation detail
}
class [[deprecated("use v2::some_class")]] some_class
{
// ...
};
} // namespace v1
namespace v2 {
namespace detail {
// implementation detail
}
class some_class
{
// ...
};
} // namespace v2
} // namespace ns
So far all well and good. However when another detail
within parent namespace gets introduced, all goes to hell:
namespace ns {
namespace detail {
class impl {};
} // namespace detail
} // namespace ns
void f()
{
ns::detail::impl i; // error: 'detail': ambiguous symbol
}
I want content ns::v1::detail
to get merged into ns::detail
, not to conflict with it.
P.S. Looks like it happens on MSVC only. Just checked with GCC and Clang, they produce no such error. Which compiler is correct in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论