Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2006-09-11 13:41:01


Realdodo Du wrote:
> I'm not sure the meaning of "wider questions". Could anyone tell me
> what is the question and the differences between global function
> pointers and member function pointers, please?

OK let me try and explain:

Function traits only recognises function types, for example given:

typedef int ft(int);

ft is a function type whose type is "int (int)", you can declare a function
with it:

ft myproc; // declares int myproc(int)

but that's about it.

In contrast a function pointer:

int (*)(int)

or a function reference:

int (&)(int)

can *not* be passed to function_traits, but you can always use
remove_pointer or remove_reference respectively to convert them to something
that you can.

Now, for member-functions there is no such type as "member function" only
pointers to member functions, for example:

int (myclass::*)(int)

You cannot pass such a type to function_traits, and you can't use
remove_pointer to convert it to something that you can either.

Now... if you try and extend function_traits to deal with
member-function-pointers the you get all kinds of tricky questions, like
"what is the functions arity?" - does the hidden "this" pointer count in
other words.

Then you have to decide what to do about const or volatile qualified
functions etc.

So... the question might be, why not a remove_member_pointer trait that
converts:

T (U::*) to T

except that no one seems to have thought of let alone needed such a beast
until now.

So.... the follow up question might be, since your code has to handle
member-function-pointers differently from regular function-pointers, why not
change:

template <class T>
void f(T f)
{
  typedef function_traits<typename remove_pointer<T>::type> ft;
  // use ft here...
}

to

template <class T, class U>
void f(T (U::*))
{
  typedef function_traits<T> ft;
  // use ft here, T is deduced as a function type
}

which obviously may or may not look anything like your actual code :-)

To conclude, how should this best be handled?

John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk