0

クラスオブジェクトを削除しようとしていますlist<boost::any> l

l.remove(class_type);

このようなものをメンバー関数として書いてみました

bool operator == (const class_type &a) const //not sure about the arguments
{
   //return bool value
}

の std::list から class のオブジェクトを削除するオーバーロード関数をどのように記述しますboost::anyか?

4

1 に答える 1

2

の署名は問題ないように見えますが、魔法のように使用しないoperator==ため、それをオーバーロードするだけでclass_typeは十分ではありません。boost::anyただし、要素を削除するには、次のように述語をremove_ifに渡すことができます。

template<class T>
bool test_any(const boost::any& a, const T& to_test) {
    const T* t = boost::any_cast<T>(&a);
    return t && (*t == to_test);
}

std::list<boost::any> l = ...;
class_type to_test = ...;
l.remove_if(boost::bind(&test_any<class_type>, _1, to_test));
于 2010-07-23T05:53:08.987 に答える