比较两组std :: weak_ptr [英] Comparing two sets of std::weak_ptr

查看:738
本文介绍了比较两组std :: weak_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用GCC 4.7.2比较两组C ++ 11 weak_ptr 。下面的代码显示了再现错误的最小可能示例:

  std :: set< std :: weak_ptr< int> std :: owner_less< std :: weak_ptr< int> > > set1; 
std :: set< std :: weak_ptr< int>,std :: owner_less< std :: weak_ptr< int> > > set2;

bool result =(set1 == set2);

尝试在上面的结果中编译一长串错误,其中以下是第一个实际错误:

  / usr / include / c ++ / 4.7 / bits / stl_algobase.h:791:6:错误: 'operator =='in'__first1.std :: _ Rb_tree_const_iterator< _Tp> :: operator *< std :: weak_ptr< int> >()== __first2.std :: _ Rb_tree_const_iterator< _Tp> :: operator *< std :: weak_ptr< int> >()'

由于 weak_ptr



一个建议是使用:

  bool result =!(set1< set2)|| < set1))

这会导致:

  / usr / include / c ++ / 4.7 / bits / stl_algobase.h:882:6:错误:没有匹配'operator'<'in __first1.std :: _ Rb_tree_const_iterator< _Tp> ; :: operator *< std :: weak_ptr< int> >() __first2.std :: _ Rb_tree_const_iterator< _Tp> :: operator *< std :: weak_ptr< int> >()'


解决方案

由于weak_ptr不支持'==',但是在这种情况下你可以使用set的比较运算符try:

  bool result =! :: lexicographical_compare(set1.begin(),set1.end(),
set2.begin(),set2.end(),
set1.value_comp())||
std: :lexicographical_compare(set2.begin(),set2.end(),
set1.begin(),set1.end(),
set1.value_comp()));

这将测试等价性,它缺乏一定的清晰度。


I am trying to compare two sets of C++11 weak_ptr using GCC 4.7.2. The code below shows the smallest possible sample reproducing the error:

std::set<std::weak_ptr<int>, std::owner_less<std::weak_ptr<int> > > set1;
std::set<std::weak_ptr<int>, std::owner_less<std::weak_ptr<int> > > set2;

bool result = (set1 == set2);

Trying to compile the above results in a long list of errors, of which the following is the first actual error:

/usr/include/c++/4.7/bits/stl_algobase.h:791:6: error: no match for ‘operator==’ in ‘__first1.std::_Rb_tree_const_iterator<_Tp>::operator*<std::weak_ptr<int> >() == __first2.std::_Rb_tree_const_iterator<_Tp>::operator*<std::weak_ptr<int> >()’

Due to the transient nature of weak_ptr, is comparing an entire set of them just not possible?

Update:

One suggestion is to use:

bool result = !((set1 < set2) || (set2 < set1))

This results in:

/usr/include/c++/4.7/bits/stl_algobase.h:882:6: error: no match for ‘operator<’ in ‘__first1.std::_Rb_tree_const_iterator<_Tp>::operator*<std::weak_ptr<int> >() < __first2.std::_Rb_tree_const_iterator<_Tp>::operator*<std::weak_ptr<int> >()’

解决方案

Since weak_ptr doesn't support '==', but in this case you can use the comparison operator of the set try:

bool result = !(std::lexicographical_compare(set1.begin(), set1.end(),
                                         set2.begin(), set2.end(),
                                         set1.value_comp()) ||
                std::lexicographical_compare(set2.begin(), set2.end(),
                                         set1.begin(), set1.end(),
                                         set1.value_comp()));

This will test for equivalence, not equality. And it lacks a certain... clarity.

这篇关于比较两组std :: weak_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆