c++ - 在 C++ 中,是否可以为所有具有散列成员函数的类定义一个专门的 std::hash?
问题描述
以下代码显示了我想要实现的目标,但我不确定在 C++ 中是否可行。
class Hashable {
public:
virtual std::size_t hash() const = 0;
};
class A : public Hashable {
std::size_t hash() override const {
...
return hash_value;
}
};
class B : public Hashable {
std::size_t hash() override const {
...
return hash_value;
}
};
namespace std {
template<>
struct hash<typename T /*where T matches all type that has hash member function*/> {
std::size_t operator()(const T& t) {
return t.hash();
}
};
}
解决方案
推荐阅读
- python - 使用 Python 在 Excel 数据验证中自动完成
- javascript - 嵌套输入问题
- laravel - 更改 JWT 用户数据结果的响应
- python-3.x - 添加表情符号 (Discord.py)
- javascript - js上的自动停止计时器
- microsoft-graph-api - OneDrive 权限 - 使用 Microsoft Graph API 获取完整数据
- html - 使用“输入”标签输入文本
- r - R中的sqrt(9)和sqrt(x = 9)有什么区别?
- shell - 按名称杀死特定端口上的进程
- mysql - RShiny 中的 For 循环:警告:“名称错误:0 个参数传递给 ... 需要 1 个”