views:

90

answers:

1

I'm trying to remove a class object from list<boost::any> l

l.remove(class_type);

I tried writing something like this as a member function

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

How would you write an overload function to remove an object of class from a std::list of boost::any?

+2  A: 

While your signature for operator== looks fine, overloading it for class_type isn't enough as boost::any doesn't magically use it. For removing elements however you can pass a predicate to remove_if, e.g.:

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));
Georg Fritzsche
Nice idea. That said, I'd reduce lines 2 and 3 to `return t `
R Samuel Klatchko