我专门针对数据类型使用“更少”(谓词)。
代码如下所示:
template<>
struct std::less<DateTimeKey>
{
bool operator()(const DateTimeKey& k1, const DateTimeKey& k2) const
{
// Some code ...
}
};
编译(Ubuntu 9.10 上的 g++ 4.4.1)时,出现错误:
‘template struct std::less’ 在不同命名空间中的特化
我做了一些研究,发现有一个“解决方法”涉及将专业化包装在 std 命名空间中 - 即将代码更改为:
namespace std {
template<>
struct less<DateTimeKey>
{
bool operator()(const DateTimeKey& k1, const DateTimeKey& k2) const
{
// Some code ...
}
};
}
这确实关闭了编译器。但是,该解决方案来自一个 5 岁的帖子(由“伟大的”维克多·巴扎罗夫(Victor Bazarof)提出[双关语无意])。这个修复仍然是要走的路,还是有更好的方法来解决这个问题,或者“旧方法”仍然有效?
原文由 Stick it to THE MAN 发布,翻译遵循 CC BY-SA 4.0 许可协议
这仍然是这样做的方法。不幸的是,您不能像使用类那样在名称空间中声明或定义函数:您需要将它们实际包装在名称空间块中。