首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::hash <std::unique_ptr>

template<class T, class Deleter> struct hash<unique_ptr<T, Deleter>>;

?

(since C++11)

模板的专门化std::hashstd::unique_ptr<T, Deleter>允许用户获取类型对象的散列。std::unique_ptr<T, Deleter>...

The specialization std::hash<std::unique_ptr<T,D>> is enabled (see std::hash) if std::hash<typename std::unique_ptr<T,D>::pointer> is enabled, and is disabled otherwise.

(since C++17)

启用后,自C++17%29以来的%28用于给定std::unique_ptr<T, D> p,此专门化确保std::hash<std::unique_ptr<T, D>>()(p)==std::hash<typenamestd::unique_ptr<T, D>::pointer>()(p.get())...

这种专门化的成员函数不能保证为no,除非指针可能是一个花哨的指针,并且它的散列可能会抛出。

二次

代码语言:javascript
复制
#include <iostream>
#include <memory>
#include <functional>
 
struct Foo {
    Foo() { std::cout << "Foo...\n"; }
    ~Foo() { std::cout << "~Foo...\n\n"; }
};
 
int main()
{
    Foo* foo = new Foo();
    std::unique_ptr<Foo> up(foo);
 
    std::cout << "hash(up):  " << std::hash<std::unique_ptr<Foo>>()(up) << '\n';
    std::cout << "hash(foo): " << std::hash<Foo*>()(foo) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Foo...
hash(up):  3686401041
hash(foo): 3686401041
~Foo...

二次

另见

hash (C++11)

hash function object (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com