functors .

V

vsgdp

Hi,

I need to pass a member function of the form C::foo(B* b, A& a) to a generic
function:

template<typename Func>
void CLASS2::bar(Func f)
{
...

f(b, a);
}


void C::g()
{
....

class2->bar( C::foo );
}

It does not work. I tried mem_fun, but that also did not work. Effective
STL page 175 gives an example of mem_fun when the function takes no
parameters.

error C2784: 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result
(__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for
'overloaded function type' from 'overloaded function type'
 
B

benben

vsgdp said:
Hi,

I need to pass a member function of the form C::foo(B* b, A& a) to a
generic function:

template<typename Func>
void CLASS2::bar(Func f)
{
...

f(b, a);
}


void C::g()
{
...

class2->bar( C::foo );
}

It does not work. I tried mem_fun, but that also did not work. Effective
STL page 175 gives an example of mem_fun when the function takes no
parameters.

error C2784: 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result
(__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument
for 'overloaded function type' from 'overloaded function type'


The easiest way is to look at the function template from boost.

Ben
 
R

Rolf Magnus

vsgdp said:
Hi,

I need to pass a member function of the form C::foo(B* b, A& a) to a
generic function:

You cannot pass a function, you can only pass a pointer to it.
template<typename Func>
void CLASS2::bar(Func f)
{
...

f(b, a);
}


void C::g()
{
...

class2->bar( C::foo );

class2->bar( &C::foo );
}

It does not work.

Well, how would the compiler know which object to call the member function
for? You have to specify that. The syntax for calling a member function
through pointer is:

(object.*function)(parameters);

or for a pointer to the object:

(object->*function)(parameters);
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top