-->

提高:: UUID的UUID ::中的std :: unordered_map的关键?(boost:

2019-09-02 10:43发布

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

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

/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不兼容? 还是我做错了什么? 任何变通办法?

Answer 1:

为什么错误的提升? 你应该专注的std ::哈希模板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_mapboost::hash面值

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

或提供hash函数对象满足要求std::hash (感谢禁卫军)。



文章来源: boost::uuids::uuid as a key in std::unordered_map?