4

weak_ptrGCC4.7.2を使用して2セットのC++11を比較しようとしています。以下のコードは、エラーを再現する可能な最小のサンプルを示しています。

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それらのセット全体を比較することは不可能ですか?

アップデート:

1つの提案は使用することです:

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> >()’
4

1 に答える 1

4

weak_ptrは'=='をサポートしていないため、この場合、settryの比較演算子を使用できます。

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()));

これは、平等ではなく、同等性をテストします。そして、それは特定の...明快さを欠いています。

于 2012-12-19T19:51:44.363 に答える