我想知道是否可以在C++中的unordered_map容器中使用对象引用作为键.
#include <unordered_map>
class Object {
int value;
};
struct object_hash {
inline size_t operator()(const Object& o) const { return 0; }
};
std::unordered_map<Object&, int, object_hash> map;
Run Code Online (Sandbox Code Playgroud)
在尝试编译这个简单的代码片段时,我遇到了一些关于方法重新定义的错误:
使用clang和libc ++
/ usr/include/c ++/v1/unordered_map:352:12:错误:类成员无法重新声明
size_t operator()(const _Cp&__ x)const
使用gcc 4.6和libstdc ++
/usr/include/c++/4.6/bits/hashtable_policy.h:556:5:错误:'std :: __ detail :: _ Map_base <_Key,_Pair,std :: _ Select1st <_Pair>,true,_Hashtable> :: mapped_type&std :: __ detail :: _ Map_base <_Key,_Pair,std :: _ Select1st <_Pair>,true,_Hashtable> :: operator [with _Key = Object&,_ Pair = std :: pair,_Hashtable = std :: …