Sign in to follow this  
fastcall22

[SOLVED] boost::mem_fn, boost::bind, std::remove_if problem

Recommended Posts

I'm experimenting with some complicated concepts that neither I nor my faithful C++ book have dared to touch. ;) Sorry if this sounds noob-ish, but it's my first time attempting to understand it. Here's an example of what I'm trying to do (based off the example code on boost.org)...
class ent_base
{
public:
	virtual bool step( const double &delta_time ) = 0;
	virtual void draw( const double &interpolate_factor ) = 0;
};

class game
{
private:
	boost::ptr_list<ent_base> m_objects;

public:
	void draw( const double &interpolate_factor )
	{
		// This is simple enough :)  Call draw(interpolate_factor) for each object
		std::for_each( m_objects.begin(), m_objects.end(), boost::bind( &ent_base::draw, _1, interpolate_factor ) );
	}

	bool step( const double &delta_time )
	{
		// This should work... :(  Call step(delta_time) for each object and remove it when the function returns true
		// Why doesn't this work? (C2582: 'operator =' is unavailable in 'ent_base')
		m_objects.erase( std::remove_if( m_objects.begin(), m_objects.end(), boost::bind( &ent_base::step, _1, dt ) ), m_objects.end() );			
	}	
};



The problem is, I don't know why it's trying to ent_base::operator=, and I don't think I want it to! _fastcall

Share this post


Link to post
Share on other sites
This comes from the nature of boost::ptr_list iterator, whose value type is ent_base instead of ent_base*. Since remove_if moves elements around through assignment, it will thus use ent_base::operator=.

To avoid this issue, use boost::ptr_list::erase_if.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this