Boost logo

Boost Users :

From: David Morrison (dave_at_[hidden])
Date: 2005-03-22 13:56:49


Hi,

I'm interested in using boost::bind and std::transform to iterate
through a container of member functions and initialize another container
with the results. I've read through the documentation pretty carefully
- in particular this bit from the bind documentation:

typedef void (*pf)(int);

std::vector<pf> v;

std::for_each(v.begin(), v.end(), bind(apply<void>(), _1, 5));

So I've been able to iterator through a set of *non* member functions
and intialize another container with the results. When I try the code
below, I get the following error message:

boost/bind/apply.hpp:39: must use .* or ->* to call pointer-to-member
function
    in `f (...)'

I have tried several variations involving mem_fn to try and create the
appropriate functors, but so far, no joy. Is there a known solution to
accomplish what I want to do?

Dave

#include <vector>
#include <iostream>
#include <iterator>
#include "boost/bind.hpp"
#include "boost/bind/apply.hpp"

using namespace std;
using namespace boost;

struct A {
float a(int m, float x) { return m * x;}
float b(int m, float x) { return m + x;}
float c(int m, float x) { return m - x;}
float d(int m, float x) { return m / x;}
};

typedef float (A::*pfn)(int, float);

int
main()
{
   A a;
   int m = 2;
   float x = 3.0;

   vector<pfn> v;
   vector<float> f;

   v.push_back(&A::a);
   v.push_back(&A::b);
   v.push_back(&A::c);
   v.push_back(&A::d);

   transform(v.begin(), v.end(), back_inserter(f),
            bind(apply<float>(), _1, a, m, x));

   copy(f.begin(), f.end(), ostream_iterator<float>(cout, "\n"));
}


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