gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:17:26 42 4
gpt4 key购买 nike

我在 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 中的错误,导致它与我的 C++ 库不兼容?或者我做错了什么?有什么解决方法吗?

最佳答案

为什么 boost 有问题?你应该专攻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);
}
};

}

或者,简单地创建 unordered_mapboost::hash par

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

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

关于c++ - boost::uuids::uuid 作为 std::unordered_map 中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471051/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com