3

Following on from this question

How to pass class member functions to a method in a 3rd party library?

Quick recap is I need to pass pointers to functions to the constructor of a class called moveset in a 3rd party library with the definition of

template <class Space>
moveset<Space>::moveset(particle<Space> (*pfInit)(rng*),
          void (*pfNewMoves)(long, particle<Space> &,rng*),
          int (*pfNewMCMC)(long,particle<Space> &,rng*))

The examples provided with the library are to simply define global functions for pfInit etc, let call them f,g and h. Then call smc::moveset Moveset(f,g,h) from within a controller class;

I have attempted to implement the suggestion using boost:bind. Unfortunately, I am struggling to get this to work.

class IK_PFWrapper
{
 public:

 IK_PFWrapper(Skeleton* skeleton, PFSettings* pfSettings) ;
 smc::particle<cv_state> fInitialise(smc::rng *pRng);

... 
} ;

in a controller class

IK_PFWrapper testWrapper (skeleton_,pfSettings_);
boost::function<smc::particle<cv_state> (smc::rng *)>  f = boost::bind(&IK_PFWrapper::fInitialise, &testWrapper,_1) ; 

// the 2nd and 3rd argument will be eventually be defined in the same manner as the 1st
smc::moveset<cv_state> Moveset(f, NULL, NULL); 

The resultant compiler error is,

Algorithms\IK_PFController.cpp(88): error C2664: 'smc::moveset<Space>::moveset(smc::particle<Space> (__cdecl *)(smc::rng *),void (__cdecl *)(long,smc::particle<Space> &,smc::rng *),int (__cdecl *)(long,smc::particle<Space> &,smc::rng *))' : cannot convert parameter 1 from 'boost::function<Signature>' to 'smc::particle<Space> (__cdecl *)(smc::rng *)'
with
[
 Space=cv_state
]
and
[
 Signature=smc::particle<cv_state> (smc::rng *)
]
and
[
 Space=cv_state
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Any help much appreciated

Community
  • 1
  • 1
oracle3001
  • 1,090
  • 19
  • 31

1 Answers1

0

See demote boost::function to a plain function pointer.

boost::function (that you create with boost::bind does not automatically convert to a plain old function pointer.

I suggest creating a wrapper interface that uses boost::function, ie your example (reduced to one parameter) would look something like:

template <class Space>
moveset<Space>::moveset(boost::function<particle<Space> (rng*)> pfInit)
{
    library_namespace::moveset<Space>(
        pfInit.target<particle<Space>(rng*)>()    // parameter 1
    );
}

Creating the wrapper means you only have to deal with raw function pointers in one place. Hope that helps, and apologies for any and all errors in the code snippet!

Community
  • 1
  • 1
Zero
  • 11,593
  • 9
  • 52
  • 70
  • Reading your previous [question](http://stackoverflow.com/questions/9997299/how-to-pass-class-member-functions-to-a-method-in-a-3rd-party-library) I'm not sure the above actually solves the original problem. I have provided more detail on the original question. – Zero Apr 11 '12 at 22:04