【发布时间】:2022-01-24 06:03:04
【问题描述】:
目前正在编写一个专门的标准库,我发现在特定情况下这对我来说是必要的:
namespace std
{
// key, value
template<class K, class V>
using vector_map = other_namespace::vector_map<K, V>;
// key, value, compare
template<class K, class V, class C>
using vector_map = other_namespace::vector_map<K, V, C>;
}
但是,它确实不起作用。不奇怪。但是我有什么选择来实现这一目标? 我曾考虑过使用预处理器,但我想知道你们的想法。
如果可能的话,我希望能够选择性地将模板类别名到另一个命名空间中。
解决方案(在我的情况下)是添加一个默认值而不是使用多个:
namespace std
{
// key, value, compare
template<class K, class V, class C = default_value>
using vector_map = other_namespace::vector_map<K, V, C>;
}
【问题讨论】:
-
你想达到什么目的?
other_namespace::vector_map也没有“重载”,它只是默认为C。这对你也有用吗? -
我希望具有与模板类本身与我的 using 语句所在的命名空间相同的功能。在此示例中,vector_map 位于 other_namespace,但我想将特定变体移至std:: 命名空间:
和 类型。 -
好的,谢谢!我刚刚将默认值添加到我的模板别名中,这就解决了。再次感谢
-
我不明白你为什么把这个声明放到
namespace std?你即将产生“未定义的行为”,例如见here。 -
我知道,而且我还在考虑这一切。这适用于没有定时器中断的操作系统,因此无论如何都可能没有(完整的)标准库。我目前使用
std只是因为它会为用户所熟悉。最终这不是我的决定。