我正在尝试使用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: 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> >()’
由于 weak_ptr
的暂时性质,比较整个集合是否相等是不可能的吗?
更新:
有一种建议是使用:
bool result = !((set1 < set2) || (set2 < set1))
这将导致:
/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> >()’
std::set
应该使用集合指定的排序方式,但实际上并不是这样的。 - Dave S