我正在使用 Visual Studio 2012。当我尝试这个时:
std::unordered_set<std::shared_ptr<A>> myset;
我收到此错误:
错误 C2338:C++ 标准不提供此类型的哈希。
根据标准和这个错误报告 ( https://connect.microsoft.com/VisualStudio/feedback/details/734888#tabs ) 这应该可以编译并且微软已经在 VC++11 中实现了支持。那么为什么这不起作用呢?
编辑:我如何使这项工作?我已经在链接页面上尝试了解决方法,它只是给我一个错误,说已经定义了哈希函数。果然,VC目录下“memory”文件的第1803行是这样的:
template<class _Ty>
struct hash<shared_ptr<_Ty> >
: public unary_function<shared_ptr<_Ty>, size_t>
{ // hash functor
typedef shared_ptr<_Ty> _Kty;
typedef _Ty *_Ptrtype;
size_t operator()(const _Kty& _Keyval) const
{ // hash _Keyval to size_t value by pseudorandomizing transform
return (hash<_Ptrtype>()(_Keyval.get()));
}
};
我通常不愿意责怪编译器,因为这通常是我的错,但这一次看起来他们真的搞砸了......