boost::uuids::uuid as a key in std::unordered_map?

一个人想着一个人 提交于 2019-12-19 02:43:05

问题


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

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
                                                     ~~~~~~~~~~~~~~~~~^

...

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


回答1:


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);
    }
};

}

or, simply create unordered_map with boost::hash par

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

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



来源:https://stackoverflow.com/questions/16471051/boostuuidsuuid-as-a-key-in-stdunordered-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!