C++ 比较两组std::弱ptr

C++ 比较两组std::弱ptr,c++,c++11,compare,std,stdset,C++,C++11,Compare,Std,Stdset,我试图使用GCC4.7.2比较两组C++11weak_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; b

我试图使用GCC4.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: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> >()’

/usr/include/c++/4.7/bits/stl_algobase.h:882:6:错误:由于弱_ptr不支持“==”,因此“运算符”不匹配,但在这种情况下,可以使用集合的比较运算符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()));

这将测试等效性,而不是相等性。而且它缺少某种。。。清晰性。

我对我的问题进行了编辑,对您最初的建议进行了评论。我注意到你更新了答案,我一定会尝试一下。出于教育目的,我将保留原始编辑。此外,我认为既然我指定std::owner_less作为比较运算符,那么弱_ptr是否实现“==”?@Hans:是的,我在输入它之后意识到它将落在“operator@Hans:问题是容器要求指定“==”操作符使用“std::equal”和
/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> >()’
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()));