Boost logo

Boost Users :

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


(Reply to two posts here)

damien_at_[hidden] wrote:
> Why not just use boost::function_traits ? That's what they're for.
>
> http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.function_traits

They don't work with function pointers and I'm binding to a pointer to
member function.

>
> Damien
>
>> Hello Kirit,
>>
>> to solve your specific problem, you could just do without Boost.function
>> and also use templates for operator():

This looks like a great way to solve this specific problem. I've never
seen a use for .* before. I think I understand what's going on with it now.

>>
>> template <class C, typename F>
>> class with_binder
>> {
>> C& m_this;
>> F m_fn;
>> public:
>> with_binder(C& ref, F fn)
>> : m_this(ref), m_fn(fn)
>> {
>> }
>>
>> // one parameter
>> template <typename T1>
>> const with_binder<C,F>& operator()(T1& v1) const
>> {
>> (m_this.*m_fn)(v1);
>> return *this;
>> }
>> template <typename T1>
>> const with_binder<C,F>& operator()(const T1& v1) const
>> {
>> (m_this.*m_fn)(v1);
>> return *this;
>> }
>>
>> // two parameters
>> template <typename T1, typename T2>
>> const with_binder<C,F>& operator()(T1& v1, T2& v2) const
>> {
>> (m_this.*m_fn)(v1, v2);
>> return *this;
>> }
>> template <typename T1, typename T2>
>> const with_binder<C,F>& operator()(const T1& v1, const T2& v2) const
>> {
>> (m_this.*m_fn)(v1, v2);
>> return *this;
>> }
>>
>> // ... and so on ...
>>
>> };
>>
>> template <class C, typename F>
>> with_binder<C,F> with(C& ref, F fn)
>> {
>> return with_binder<C,F>(ref,fn);
>> }
>>
>>
>> Regards,
>> Benjamin
>>
>>> 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