Boost logo

Boost Users :

From: Benjamin Winkler (Benjamin.Winkler_at_[hidden])
Date: 2007-06-13 17:55:52


Hello Kirit,

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

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 mailing list
>Boost-users_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users


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