Boost logo

Boost Users :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-06-13 03:39:16


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net