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 中删除一个类的对象boost::any

4

1 回答 1

2

虽然你的签名 foroperator==看起来不错,但重载它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 回答