为嵌套自定义类型定义散列函数
defining hash function for nested custom type
给定如下嵌套模板类结构:
template<class T> class A
{
...
public:
...
class B;
};
我希望std::哈希函数在嵌套类上工作,这样它就可以放在unsorted_map和unsorted_set中,所以我将嵌套类定义为:
template<class T> class A<T>::B
{
public:
bool operator==(const A<T>::B &) const;
...
friend struct std::hash<A<T>::B>;
};
然后尝试为该类型添加一个特殊的std::散列结构,如下所示:
namespace std
{
template<class T> struct hash<A<T>::B>
{
bool operator()(const A<T>::B &x) const
{
...
}
};
}
但是当我尝试像这样定义自定义std::hash函数对象时,编译器会强烈地抱怨。
我得到的错误信息是无用的,如下所示:
xyz.cc:17:38: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp> struct std::hash'
template<class T> struct hash<A<T>::B>
^
xyz.cc:17:38: error: expected a type, got 'A<T>::B'
我不确定我应该如何表达它,然而。
为什么这是错误的,我必须做什么来修复它?
你不能。
依赖类型不能进行模式匹配,因为一般情况下需要反转任意的图灵完全算法。
最简单的方法是使B
成为一个独立的模板,然后将pedef类型化为A
。
相关文章:
- 为非标准对象定义散列函数和相等函数
- 对于自定义类的一个未定义集,是否有一个默认的散列函数
- 字典中使用了什么散列函数(hash_table)
- 指针的最快散列函数是什么
- 一个很好的向量散列函数
- 获得快速的k对独立散列函数的选项是什么
- 避免散列函数中的严格混叠冲突
- 当我在Unorder容器中使用位集时,散列函数是什么样子的
- 这个简单的散列函数背后的逻辑是什么
- 很好的散列函数可以从数组中删除重复项
- uint32_t值对的交换散列函数
- 带有自定义散列函数(bstrt)的无序映射仅适用于默认构造函数(重复键)
- 如何在散列函数中选取基数
- 使用unordered_set时,struct中嵌套结构的散列函数
- c++使用[](int i){return i;}作为unordered_set散列函数
- boost::unordered_map -- 需要为散列 std::set 指定自定义散列函数<int>吗?
- Unordered_map vs vector +针对少量元素的自定义散列
- 为嵌套自定义类型定义散列函数
- 使用lambda定义散列函数会引发异常
- 用比较函数定义散列集