[boost::function_types] Create function type from member function type
::type arg_signature;
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
Now, I used this helper class:
// Converts R (X::)(Args) to R (Args)
template<class F>
class UnMember
{
typedef boost::function_type_signature<F> member_signature;
typedef typename boost::mpl::erase
Peter Soetens wrote:
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
You might want to read the (excellent) article on function pointers in C++ at http://www.codeproject.com/cpp/FastDelegate.asp. That will explain why your question is highly nontrivial, but a worthy query for certain applications (like delegates). Rod
Rodney Morison wrote:
Peter Soetens wrote:
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
You might want to read the (excellent) article on function pointers in C++ at http://www.codeproject.com/cpp/FastDelegate.asp. That will explain why your question is highly nontrivial, but a worthy query for certain applications (like delegates).
The example suite (link in my previous post in this thread) coincidentally contains a member function wrapper that uses member function pointers as non-type template parameters. That technique could be used to build an equally fast delegate without relying upon compiler internals. Regards, Tobias
On Friday 11 August 2006 23:33, Rodney Morison wrote:
Peter Soetens wrote:
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
You might want to read the (excellent) article on function pointers in C++ at http://www.codeproject.com/cpp/FastDelegate.asp. That will explain why your question is highly nontrivial, but a worthy query for certain applications (like delegates).
Thanks for this interesting link. Except for the adaptation of boost::function, I wonder if C++0x has any plans for adding delegates ? The dynamic memory allocation that boost::function requires (upon 'binding') is also a pain for my applications. Peter -- Peter Soetens -- FMTC -- http://www.fmtc.be
Peter Soetens wrote:
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
[code]
Is there an easier way to do this ?
Yes. If your compiler behaves you can use template partial specialization for a member pointer to do this transformation. Unfortunately, it's not very portable, currently. It's the reason to use FunctionTypes here in the first place. Using the upcoming version (preview in the vault http://tinyurl.com/qpffx) of FunctionTypes your "unmember" template would look like this: template<typename F> struct unmember : function_pointer < mpl::joint_view< mpl::single_view< typename result_type<F>::type > , typename mpl::pop_front< parameter_types<F> >::type > > { }; Things are a little different with the older version you appear to be using: a) omit mpl::pop_front (parameter_types<F> includes the class type, now) b) use typename function_type_parameters<F>::type (note: typename...type is needed, because function_type_parameters<F> does not model an MPL sequence, only its type member does) BTW: Not removing the class type (creating a function that takes the class context in form of a properly cv-qualified reference) is even simpler: function_pointer<F>::type // is 'bool (*)(X &,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const &,int)' for F = 'bool (X::*)(int) const' To encourage you to use the new version and because the documentation is still not finished, I just uploaded the source code examples: http://tinyurl.com/mkjv7 Regards, Tobias
On Saturday 12 August 2006 23:21, Tobias Schwinger wrote:
Peter Soetens wrote:
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
[code]
Is there an easier way to do this ?
Yes.
If your compiler behaves you can use template partial specialization for a member pointer to do this transformation. Unfortunately, it's not very portable, currently. It's the reason to use FunctionTypes here in the first place.
Using the upcoming version (preview in the vault http://tinyurl.com/qpffx) of FunctionTypes your "unmember" template would look like this:
template<typename F> struct unmember
: function_pointer
< mpl::joint_view< mpl::single_view< typename result_type<F>::type > , typename mpl::pop_front< parameter_types<F>
::type
{ };
Thanks for this update, I really appreciate the work you are putting in this library, and it effectively helps me to solve 'delegate-like' problems in my application framework.
Things are a little different with the older version you appear to be using: a) omit mpl::pop_front (parameter_types<F> includes the class type, now) b) use typename function_type_parameters<F>::type (note: typename...type is needed, because function_type_parameters<F> does not model an MPL sequence, only its type member does)
I'm distributing the function_types headers with my library (until it gets into boost), so I can include a new version if needed so.
BTW: Not removing the class type (creating a function that takes the class context in form of a properly cv-qualified reference) is even simpler:
function_pointer<F>::type // is 'bool (*)(X &,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const &,int)' for F = 'bool (X::*)(int) const'
Thanks, but I wanted the X& to be a pointer as well. OTOH, maybe I can change my code to pass a reference to X.
To encourage you to use the new version and because the documentation is still not finished, I just uploaded the source code examples:
Examples are of great help to grasp the possibilities or syntax of this library. Peter -- Peter Soetens -- FMTC -- http://www.fmtc.be
Peter Soetens wrote:
On Saturday 12 August 2006 23:21, Tobias Schwinger wrote:
Peter Soetens wrote:
Hi,
I was looking for a way to convert a member function type:
bool (X::*)(int) or bool (X::)(int)
to the 'C' equivalent, without the class pointer:
bool (*)(int) or bool(int)
[code]
Is there an easier way to do this ?
Yes.
If your compiler behaves you can use template partial specialization for a member pointer to do this transformation. Unfortunately, it's not very portable, currently. It's the reason to use FunctionTypes here in the first place.
Using the upcoming version (preview in the vault http://tinyurl.com/qpffx) of FunctionTypes your "unmember" template would look like this:
template<typename F> struct unmember
: function_pointer
< mpl::joint_view< mpl::single_view< typename result_type<F>::type > , typename mpl::pop_front< parameter_types<F>
::type
{ };
Thanks for this update, I really appreciate the work you are putting in this library, and it effectively helps me to solve 'delegate-like' problems in my application framework.
I'm glad to hear that!
BTW: Not removing the class type (creating a function that takes the class context in form of a properly cv-qualified reference) is even simpler:
function_pointer<F>::type // is 'bool (*)(X &,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const &,int)' for F = 'bool (X::*)(int) const'
Thanks, but I wanted the X& to be a pointer as well. OTOH, maybe I can change my code to pass a reference to X.
function_pointer<F>::type is a shortcut for function_pointer< components<F> >::type and 'components' has a second template parameter, an MPL Lambda Expression, that allows to change the way the class type is transformed (and so has 'parameter_types'): function_pointer< components< F, add_pointer<_> >::type // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const *,int)' for F = 'bool (X::*)(int) const' function_pointer< components< F, add_pointer< remove_cv<_> > >::type // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int) const' Regards, Tobias
On Friday 18 August 2006 08:34, Tobias Schwinger wrote:
Peter Soetens wrote:
On Saturday 12 August 2006 23:21, Tobias Schwinger wrote: [...]
BTW: Not removing the class type (creating a function that takes the class context in form of a properly cv-qualified reference) is even simpler:
function_pointer<F>::type // is 'bool (*)(X &,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const &,int)' for F = 'bool (X::*)(int) const'
Thanks, but I wanted the X& to be a pointer as well. OTOH, maybe I can change my code to pass a reference to X.
function_pointer<F>::type
is a shortcut for
function_pointer< components<F> >::type
and 'components' has a second template parameter, an MPL Lambda Expression, that allows to change the way the class type is transformed (and so has 'parameter_types'):
function_pointer< components< F, add_pointer<_> >::type // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const *,int)' for F = 'bool (X::*)(int) const'
function_pointer< components< F, add_pointer< remove_cv<_> > >::type // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int) const'
This is a complete killer-template. You are a genious. If this becomes the 'look-and-feel' interface of the whole library (instead of the 'tags'), it will be an example for many other meta programming libraries. Peter -- Peter Soetens -- FMTC -- http://www.fmtc.be
Peter Soetens wrote:
On Friday 18 August 2006 08:34, Tobias Schwinger wrote:
Peter Soetens wrote:
On Saturday 12 August 2006 23:21, Tobias Schwinger wrote:
BTW: Not removing the class type (creating a function that takes the class context in form of a properly cv-qualified reference) is even simpler:
function_pointer<F>::type // is 'bool (*)(X &,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const &,int)' for F = 'bool (X::*)(int) const'
Thanks, but I wanted the X& to be a pointer as well. OTOH, maybe I can change my code to pass a reference to X.
function_pointer<F>::type
is a shortcut for
function_pointer< components<F> >::type
and 'components' has a second template parameter, an MPL Lambda Expression, that allows to change the way the class type is transformed (and so has 'parameter_types'):
function_pointer< components< F, add_pointer<_> >::type // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X const *,int)' for F = 'bool (X::*)(int) const'
function_pointer< components< F, add_pointer< remove_cv<_> > >::type // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int)' // is 'bool (*)(X *,int)' for F = 'bool (X::*)(int) const'
This is a complete killer-template. You are a genious.
Thanks. It's MPL that makes it possible, so I have to pass some of the fame to Aleksey. The review brought up the points that led to the current design so another portion goes to the reviewers...
If this becomes the 'look-and-feel' interface of the whole library (instead of the 'tags'), it will be an example for many other meta programming libraries.
Well there are still tags, but in many cases you don't have to care about
them. Further there are only "atomary" tags, that is they can be combined
by the user. Here are some example use cases:
function_pointer
participants (3)
-
Peter Soetens
-
Rodney Morison
-
Tobias Schwinger