简体   繁体   English

std :: map with std :: weak_ptr key

[英]std::map with std::weak_ptr key

I have a question about using a std::weak_ptr as a key for a std::map. 我有一个关于使用std :: weak_ptr作为std :: map的键的问题。

#include <map>
#include <memory>

int main()
{
std::map< std::weak_ptr<int>, bool > myMap;

std::shared_ptr<int> sharedptr(new int(5));
std::weak_ptr<int> weakptr = sharedptr;

myMap[weakptr] = true;

return 0;
}

The above program doesn't build and trying to compile gives many error messages such as: 上面的程序没有构建,并且尝试编译会给出许多错误消息,例如:

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::tr1::weak_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(1885) : see declaration of 'std::operator <'
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1>          with
1>          [
1>              _Ty=std::tr1::weak_ptr<int>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=std::tr1::weak_ptr<int>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1>          with
1>          [
1>              _Kty=std::tr1::weak_ptr<int>,
1>              _Ty=bool,
1>              _Pr=std::less<std::tr1::weak_ptr<int>>,
1>              _Alloc=std::allocator<std::pair<const std::tr1::weak_ptr<int>,bool>>,
1>              _Mfl=false
1>          ]

The problem occurs due to the following line: 由于以下行发生此问题:

myMap[weakptr] = true;

The error messages seem to be related to operator<. 错误消息似乎与operator <有关。 Do I need to define operator< for weak_ptrs? 我是否需要为weak_ptrs定义operator <? Exactly what operators need to be defined in order to use a data type as a key for a std::map? 究竟是什么运算符需要定义才能使用数据类型作为std :: map的键?

(I should note that I have already defined operator== in the std namespace. Also, I plan to use a weak_ptr to a custom class type and not an int.) (我应该注意到我已经在std命名空间中定义了operator ==。另外,我计划将weak_ptr用于自定义类类型而不是int。)

C++11 provides appropriate mechanisms for comparisons of std::weak_ptr , namely: std::owner_less . C ++ 11提供了适当的机制来比较std::weak_ptr ,即: std::owner_less

This should be the default for maps and sets. 这应该是地图和集合的默认值。 If the C++ compiler you are using is having a hard time, try using std::owner_less if it is available. 如果您使用的C ++编译器很难,请尝试使用std::owner_less如果可用)。 If it is not available, you will need to provide a similar mechanism as std::owner_less so you can appropriately compare std::weak_ptr objects. 如果它不可用,您将需要提供与std::owner_less类似的机制,以便您可以适当地比较std::weak_ptr对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM