Boost logo

Boost Users :

From: stupid_newcomer (stupid_newcomer_at_[hidden])
Date: 2002-08-25 10:20:47


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!


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