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

2023-11-26

我在 Mac OS X 上使用 clang (CXX='clang++ -std=c++11 -stdlib=libc++'),使用 boost 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
                                                     ~~~~~~~~~~~~~~~~~^

...

这是什么 - Boost 中的一个错误,导致它与我的 C++ 库不兼容?或者我做错了什么?有什么解决方法吗?


为什么 boost 会出现错误?你应该专攻标准::散列模板为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 with boost::hash par

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

或提供hash函子满足以下要求std::hash(感谢禁卫军)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

boost::uuids::uuid 作为 std::unordered_map 中的键? 的相关文章

随机推荐