I've been playing around with a few functional programming idioms in C++ using Boost.function and Boost.lambda and was wondering if it was possible to fetch out the argument type (i.e. int) from a type like this: void (*somefunc)( int ) It looks to me like boost::function<> might be doing something along these lines. I've been working on a version of 'with' for use in initialising lists etc. At the moment you need to do this: std::list< int > list1; list1.push_back( 3 ); list1.push_back( 1 ); list1.push_back( 4 ); But using 'with' you can do this: with( list2, &std::list< int >::push_back )( 3 )( 1 )( 4 )( 1 )( 5 ); The implementation I have right now is fairly simple: template< typename F > struct with_binder { with_binder( F f ) : m_f( f ) { } const with_binder &operator()( int i ) const { m_f( i ); return *this; } private: F m_f; }; template< typename O, typename F > inline with_binder< boost::function< void ( int ) > > with( O &o, F f ) { return with_binder< boost::function< void ( int ) > >( boost::lambda::bind( f, &o, boost::lambda::_1 ) ); } But the 'int' argument type is hard coded. I could have it as the first template parameter to 'with' and simply require that it is given when using 'with', but it would neater if it could be derived from the type 'F' in 'with'. Clearly to be more generally useful it also needs to be extended for multiple parameters. K
"Kirit Sælensminde"
I've been playing around with a few functional programming idioms in C++ using Boost.function and Boost.lambda and was wondering if it was possible to fetch out the argument type (i.e. int) from a type like this:
void (*somefunc)( int )
You don't need any libraries for this -- just use partial template
specialization:
template<class T> struct deduce_arg; //not defined
template<class A> struct deduce_arg
Arkadiy Vertleyb wrote:
"Kirit Sælensminde"
wrote I've been playing around with a few functional programming idioms in C++ using Boost.function and Boost.lambda and was wondering if it was possible to fetch out the argument type (i.e. int) from a type like this:
void (*somefunc)( int )
You don't need any libraries for this -- just use partial template specialization:
template<class T> struct deduce_arg; //not defined
template<class A> struct deduce_arg
{ typedef A0 type; };
This is cool. I'm sure I've tried this sort of thing in MSVC 7.1 before and not had it work. I guess I got the syntax all mixed up. You have a typo in the deduction part. A0 should be A (or A should be A0). It also needs to be a little different to work with the member function (I've added the return type R to the deduction, but the important bit is the extra specialisation): template< typename T > struct deduce_arg; template< typename R, typename A > struct deduce_arg< R (*) (A) > { typedef R ret; typedef A arg1; }; template< typename C, typename R, typename A > struct deduce_arg< R (C::*) (A) > { typedef R ret; typedef A arg1; }; Thanks for the idea. K
participants (2)
-
Arkadiy Vertleyb
-
Kirit Sælensminde