Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] named parameter technique with fusion::map
From: Larry Evans (cppljevans_at_[hidden])
Date: 2010-11-07 17:11:12


On 11/07/10 13:07, Alfredo Correa wrote:
[snip]
> template<double(f)(double)>
> double derivative(double const& x){ ...already defined... }
>
> double f(double x){return x*x;}
>
> used as
> derivative<f>(1.);
>
> the question is how to define a function "d" that takes a function like
> H with fusion container arguments, bind it against all arguments except
> one and calls derivative on that function. For example
>
> d<H, q>(1.,2.);
>
> internally, the code should bind all the parameters except q to its
> corresponding values in the argument. in this case it is H binded with
> p=1. and then *derivative* of H( __, 2.) is called with argument 1.
>
[snip]
What about just creating a fusion::vector for tuple containing
an initial value, then store that into a templated functor taking the
free variable index as one of the template arguments, and then the
operator()(double value) would use value to set the tuple at the
template index and then call the function. For example:

//{-- free_at.hpp --

  template
  < typename Tuple//e.g. fusion::vector<T1,T2,...Tn>
  < typename Functor//functor taking value of type Tuple.
  , unsigned FreeIndex//the index of the Tuple component that's "free"
>
struct free_at
: Tuple
{
      Functor
    functor
    ;
    template<typename... Args>
    free_at(Functor f, Args... args)
    : Tuple(args...)
    , functor(f)
    {}

    template<unsigned FreeOther>
    free_at
      ( free_at
          < Tuple
          , Functor
          , FreeOther
>const&
        free_other
      )
      //Change the free_variable from FreeOther to FreeIndex
      : Tuple(free_other)
      , functor(free_other.f)
    {}

    template<typename FreeValu>
      typename Functor::result_type
    operator()(FreeValue free_value)
    {
        fusion::at_c<FreeIndex>(*this) = free_value;
        return functor(*this);
    }
}:
//}-- free_at.hpp --

Of course the above code uses variadic template notation, but I
hope it's clear anyway. Also, Functor is assumed to take
a Tuple as an argument. Obviously if the actual function
used doesn't do that, then Functor could just contain a
pointer to the actual function and expand the Tuple arguments
in a call to the actual function.

HTH.

-Larry


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