提高:: ::的UUID UUID作为中的std :: unordered_map的关键? [英] boost::uuids::uuid as a key in std::unordered_map?

查看:405
本文介绍了提高:: ::的UUID UUID作为中的std :: unordered_map的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用铛(CXX ='铛++ -std = C ++ 11 -stdlib = libc中++')在Mac OS X,与升压1.53.0。

I'm using clang (CXX='clang++ -std=c++11 -stdlib=libc++') on Mac OS X, with boost 1.53.0.

我想用UUID作为unordered_map键,但得到以下错误:

I want to use uuid as keys in unordered_map, but getting the following errors:

/usr/bin/../lib/c++/v1/type_traits:748:38: error: implicit instantiation of undefined template
      'std::__1::hash<boost::uuids::uuid>'
    : public integral_constant<bool, __is_empty(_Tp)> {};
                                 ^
/usr/bin/../lib/c++/v1/unordered_map:327:54: note: in instantiation of template class
      'std::__1::is_empty<std::__1::hash<boost::uuids::uuid> >' requested here
template <class _Key, class _Tp, class _Hash, bool = is_empty<_Hash>::value

...

/usr/bin/../lib/c++/v1/unordered_map:327:71: error: no member named 'value' in
      'std::__1::is_empty<std::__1::hash<boost::uuids::uuid> >'
template <class _Key, class _Tp, class _Hash, bool = is_empty<_Hash>::value
                                                     ~~~~~~~~~~~~~~~~~^

...

这是什么 - 升压中的一个错误,这使得它与我的C ++的lib不兼容?还是我做错了什么?任何变通办法?

What is it - a bug in Boost, which makes it incompatible with my C++ lib? Or I am doing something wrong? Any workarounds?

推荐答案

为什么错误的提升?你应该专注的std ::对升压哈希模板:: UUID

Why bug in boost? You should specialize std::hash template for boost::uuid.

#include <boost/functional/hash.hpp>

namespace std
{

template<>
struct hash<boost::uuids::uuid>
{
    size_t operator () (const boost::uuids::uuid& uid)
    {
        return boost::hash<boost::uuids::uuid>()(uid);
    }
};

}

,或简单地创建 unordered_map 的boost ::哈希参数

std::unordered_map<boost::uuids::uuid, T, boost::hash<boost::uuids::uuid>>

或提供仿函数满足的std ::哈希(感谢执政官)的要求。

or provide hash functor that satisfies requirements of std::hash (thanks to Praetorian).

这篇关于提高:: ::的UUID UUID作为中的std :: unordered_map的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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