std :: map with std :: weak_ptr key

use*_*280 12 c++ dictionary weak-references stdmap weak-ptr

我有一个关于使用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;
}
Run Code Online (Sandbox Code Playgroud)

上面的程序没有构建,并且尝试编译会给出许多错误消息,例如:

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>          ]
Run Code Online (Sandbox Code Playgroud)

由于以下行发生此问题:

myMap[weakptr] = true;
Run Code Online (Sandbox Code Playgroud)

错误消息似乎与operator <有关.我是否需要为weak_ptrs定义operator <?究竟是什么运算符需要定义才能使用数据类型作为std :: map的键?

(我应该注意到我已经在std命名空间中定义了operator ==.另外,我计划将weak_ptr用于自定义类类型而不是int.)

Jod*_*ins 23

C++ 11提供了适当的比较机制std::weak_ptr,即:std::owner_less.

这应该是地图和集合的默认值.如果您使用的C++编译器很难,请尝试使用std::owner_less它是否可用.如果它不可用,您将需要提供类似的机制,std::owner_less以便您可以适当地比较std::weak_ptr对象.

  • 最终的形式是`std :: map <std :: weak_ptr <T>,U,std :: owner_less <std :: weak_ptr <T >>> (8认同)

hon*_*onk 5

正如上文乔迪Hagins的回答,你应该使用std::owner_less作为关联容器的比较函数对象,如果你使用std::weak_ptr的关键。我想通过为您的代码提供以下完整解决方案来扩展该答案:

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

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

    myMap[weakptr] = true;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C++17 开始,您可以省略 的模板参数owner_less,从而得到更短的代码,如下所示:

std::map<std::weak_ptr<int>, bool, std::owner_less<>> myMap;
Run Code Online (Sandbox Code Playgroud)

如果您打算使用 aweak_ptr到自定义类而不是int,那么您可以简单地将 替换int为您的类的名称,如Coliru 上的示例代码所示

一般来说,除了operator<为(自定义)键类型提供适当的比较函数(或重载)外,不需要为std::map. operator==不需要对密钥类型进行重载。