I'm trying to play with boost::bind and boost::mem_fn, however, i have some difficulties with the member function with more than 2 arguments. I have tried a number of time and gone through the documentation , but still can't found an answer. Example: #include <boost/shared_ptr.hpp> #include <boost/mem_fn.hpp> #include <boost/bind.hpp> #include <iostream> #include <vector> #include <algorithm> using namespace std; class B { public: void print0() const { cout << "B::print0 " << endl; } void print1(int i) const { cout << "B::print1 " << i << endl; } void print2(int i, int j) const { // how to call?? cout << "B::print2 " << i << ' '<< j << endl; } }; int main() { typedef boost::shared_ptr<B> Ptr; vector<Ptr> v; for (int i = 0; i < 3; ++i) { v.push_back(boost::shared_ptr<B>(new B())); } // zero argument, OK for_each(v.begin(), v.end(), boost::mem_fn(&B::print0)); // one argument, OK for_each(v.begin(), v.end(), boost::bind(&B::print1, _1, 5)); // two argument, how to call B::print2 ? // for_each(v.begin(), v.end(), /* B::print2 */); return 0; } Thanks for your help!
On Sunday 25 August 2002 11:20 am, stupid_newcomer wrote:
// zero argument, OK for_each(v.begin(), v.end(), boost::mem_fn(&B::print0));
// one argument, OK for_each(v.begin(), v.end(), boost::bind(&B::print1, _1, 5));
// two argument, how to call B::print2 ?
for_each(v.begin(), v.end(), boost::bind(&B::print2, _1, 5, 6)); Doug
participants (2)
-
Douglas Gregor
-
stupid_newcomer