提问者:小点点

boost::uuids::uuid作为std::unordered_map中的键?


我正在Mac OS X上使用clang(cxx='clang++-std=C++11-stdlib=libc++'),使用boost 1.53.0。

我想在unordered_map中使用uuid作为键,但得到以下错误:

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

。。。

这是什么--Boost中的一个bug,使它与我的C++库不兼容?还是我做错了什么?有什么变通办法吗?


共1个答案

匿名用户

为什么在Boost中安装bug?您应该为boost::uuid专门化std::hash模板。

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

}

或者,只需使用boost::hashpar创建unordered_map

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

或者提供满足std::hash要求的hash函数(感谢Praetorian)。