Boost logo

Boost Users :

From: David Morrison (dave_at_[hidden])
Date: 2005-03-22 15:28:55


Pablo Aguilar wrote:
> 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;

Thanks, that works nicely, though the required coding difference is a
bit disconcerting. I found that if I created a class in analogy with
"apply" called, say, "apply_mem_fn" like this:

namespace boost
{

template<class R> struct apply_mem_fn
{
     typedef R result_type;

     template<class F, class A1, class A2, class A3> result_type
operator()(F & f, A1 & a1, A2 & a2, A3 & a3) const
     {
       return ((a1).*(f))(a2, a3);
     }

};

} // namespace boost

Then I can achieve what I was trying to accomplish with code that looks
nearly the same as in the case of pointers to non-member functions:

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

namespace boost
{

template<class R> struct apply_mem_fn
{
     typedef R result_type;

     template<class F, class A1, class A2, class A3> result_type
operator()(F & f, A1 & a1, A2 & a2, A3 & a3) const
     {
       return ((a1).*(f))(a2, a3);
     }

};

} // namespace boost

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_mem_fn<float>(), _1, a, m, x));

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

It's not clear to me whether it's possible to choose the right
implementation based on the argument.

Dave


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