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

rin*_*ind 7 c++ boost clang boost-uuid

我在Mac OS X上使用clang(CXX ='clang ++ -std = c ++ 11 -stdlib = libc ++'),增强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
Run Code Online (Sandbox Code Playgroud)

...

/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
                                                     ~~~~~~~~~~~~~~~~~^
Run Code Online (Sandbox Code Playgroud)

...

它是什么 - Boost中的一个错误,它使它与我的C++ lib不兼容?或者我做错了什么?任何解决方法?

For*_*veR 15

为什么提升中的bug?你应该专门为std :: hash模板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);
    }
};

}
Run Code Online (Sandbox Code Playgroud)

或者,只需创建unordered_mapboost::hash比肩

std::unordered_map<boost::uuids::uuid, T, boost::hash<boost::uuids::uuid>>
Run Code Online (Sandbox Code Playgroud)

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

  • +1除了提供`std :: hash`的显式特化之外,你还可以创建一个类型(比如`uuid_hasher`)并实现`uuid_hasher :: operator()(uuid const&)`.那个类型将是`unordered_map`的第三个模板参数 (2认同)