gpt4 book ai didi

c++ - 'template struct std::less' 在不同命名空间中的特化

转载 作者:IT老高 更新时间:2023-10-28 12:52:48 24 4
gpt4 key购买 nike

我专门针对数据类型使用“少”(谓词)。

代码如下所示:

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)提出[双关语无意])。这个修复仍然是要走的路,还是有更好的方法来解决这个问题,或者“旧方法”仍然有效?

最佳答案

这仍然是这样做的方法。不幸的是,您不能像使用类那样在命名空间中声明或定义函数:您需要将它们实际包装在命名空间 block 中。

关于c++ - 'template<class _Tp> struct std::less' 在不同命名空间中的特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2282349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com