substituting a function ptr with a function object

E

er

hi,

class Gen{
public:
Gen(double (*fun_)()):fun(fun_){};
private:
double (*fun)();
};
class Op{
public:
Op(){};
double operator()(){return 0.0;};
};
double fun(){return 0.0;};

// i'd like to be able to construct Gen from either a fun ptr (OK,
fine as is) or a function object. the code below is probably
adventurous...any suggestion?

int main(){
Gen gen1(&fun);//ok
Op op;
Gen gen2(
boost::bind(
&Op::eek:perator(),
boost::ref(op)
)
);//error: no matching function for call.//the result from bind does
not match double (*)()
 
E

er

hi,

class Gen{
public:
Gen(double (*fun_)()):fun(fun_){};
private:
double (*fun)();};

class Op{
public:
Op(){};
double operator()(){return 0.0;};};

double fun(){return 0.0;};

// i'd like to be able to construct Gen from either a fun ptr (OK,
fine as is) or a function object. the code below is probably
adventurous...any suggestion?

int main(){
Gen gen1(&fun);//ok
Op op;
Gen gen2(
boost::bind(
&Op::eek:perator(),
boost::ref(op)
)
);//error: no matching function for call.//the result from bind does
not match double (*)()

ps: i know i could redefine Gen as something like Gen(T,double (T::*)
()) and then if i happen to have a function, just wrap a functor
around it, but if possible, i'd still rather go with Gen(double (*)())
 
G

Guest

hi,

class Gen{
public:
Gen(double (*fun_)()):fun(fun_){};
private:
double (*fun)();
};
class Op{
public:
Op(){};
double operator()(){return 0.0;};
};
double fun(){return 0.0;};

// i'd like to be able to construct Gen from either a fun ptr (OK,
fine as is) or a function object. the code below is probably
adventurous...any suggestion?

Perhaps a template?

template<typename T>
class Gen {
T fun;
public:
Gen(T t) : fun(t) {}
void run() { fun(); }
};
 
B

Barry

er said:
hi,

class Gen{
public:
Gen(double (*fun_)()):fun(fun_){};
private:
double (*fun)();
};
class Op{
public:
Op(){};
double operator()(){return 0.0;};
};
double fun(){return 0.0;};

// i'd like to be able to construct Gen from either a fun ptr (OK,
fine as is) or a function object. the code below is probably
adventurous...any suggestion?

int main(){
Gen gen1(&fun);//ok
Op op;
Gen gen2(
boost::bind(
&Op::eek:perator(),
boost::ref(op)
)
);//error: no matching function for call.//the result from bind does
not match double (*)()

what
"
boost::bind(
&Op::eek:perator(),
boost::ref(op)
)
"

returns is not typeof "double (*fun_)()",
it's functor.

As Erik mentioned else thread, using template, but it's a solution
you don't even know the how to fill T in Gen<T>;

Stop reinventing another wheel, have a look at Boost.Function first,
use it, or refer to it, and write something exactly meets your need

In your case, you can use boost::function0<double> (equivalent to
boost::function<double()>) as your functor/function wrapper,
you can then use boost::bind to bind any function/functor who returns
double, or you can even write some adapter.

#include <boost/bind.hpp>
#include <boost/function.hpp>

#include <functional>

double fun() { return 0.0; }

struct Adapter
: std::unary_function<double, int>
{
double operator() (int) { return 0.0; }
};

int getint(char c) { return 0; }

int main()
{
boost::function0<double> f = &fun;
double d = f();

f = boost::bind(Adapter(), boost::bind(&getint, 10));
d = f();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top