我可以覆盖std :: hash吗? [英] Can I override std::hash?

查看:91
本文介绍了我可以覆盖std :: hash吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用我自己的 std :: hash 定义代替 std :: hash 的实际实现C ++ 11吗?

I can replace the actual implementation of std::hash with my own definition of std::hash in C++ 11 ?

我的意思是从我的代码库中,不接触标准库。

I mean from my codebase, without touching the standard library.

我看不到在这种情况下对虚函数/多态有任何用途,所以我想我还是不能更改std :: hash的定义?

I can't see any use for virtual function/polymorphism in this case, so I suppose that I can't alter the definition of std::hash anyway ?

推荐答案

没关系,您无需以任何方式修改标准库,只需使用模板特化:

Yes it's okay, and you don't have to modify the standard library in any way, just use template specialization:

namespace std
{
    template<>
    struct hash<YourSpecialType>
    {
        // ...
    };
}

这篇关于我可以覆盖std :: hash吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆