Boost logo

Boost Users :

From: Pablo Aguilar (pablo.aguilar_at_[hidden])
Date: 2005-03-22 15:04:57


Maybe not the best solution performance-wise, but you can try the following:

Replace
    typedef float (A::*pfn)(int, float);
with
    typedef boost::function3<float,A*,int,float> pfn;

then
    v.push_back(&A::a);
    ...
with
    v.push_back(boost::mem_fn(&A::a));
    ...

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

with

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

I'm not sure if its necessary to include the extra header files, because
bind might include them already, but you'll probably need:
#include <boost/mem_fn.hpp>
#include <boost/function.hpp>

HTH

Pablo Aguilar

"David Morrison" <dave_at_[hidden]> wrote in message
news:42406A71.3030803_at_bnl.gov...
> 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