Boost logo

Boost Users :

From: Bill Buklis (boostuser_at_[hidden])
Date: 2007-11-27 17:21:54


I can't figure out how to use std::mem_fun() and std::bind1st() with
std::for_each() to call into a member function for each element in a
boost::ptr_vector object. Some sample code below:

class Object
{
public:
    void Tick( float number ) {}
};

boost::ptr_vector<Object> m_objects;
using namespace std;
for_each( m_objects.begin(), m_objects.end(), bind1st( mem_fun(
&Object::Tick ), 5 ) );

Of course, this doesn't compile at all. Can anyone tell me what I'm doing
wrong?

 

Try using boost::bind. It makes life so much simpler. Once I discovered that
I never used bind1st, mem_fun, etc. again. I believe this is included in TR1
anyway.

 

for_each( m_objects.begin(), m_objects.end(), boost::bind(&Object::Tick, _1,
5.0) );

 

Although, this may give you a warning about converting a double to a float.
Why is Tick defined to take a float anyway?

 

 

-- Bill --

 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net