Boost logo

Boost Users :

Subject: Re: [Boost-users] [mpl - function_types] Generate typedef from operator() of a functor
From: joel falcou (joel.falcou_at_[hidden])
Date: 2010-01-30 10:56:21


Olivier Tournaire wrote:
>
> 2) if the operator() is overloaded, the user class currently
> has to typedef an mpl::vector of mpl::pairs such as
>
> class G {
> typedef mpl::vector<mpl::pair<T2,T3>, mpl::pair<T1,T1>,
> mpl::pair<T3,T1> > signature_type;
> double operator()(const T2& t2, T3& t3);
> double operator()(const T1& t1, T1& t1);
> double operator()(const T3& t3, T1& t1);
> };
>
> Why don't you use something like the result_of protocol ?
>
>
> Why not. Could you give us more details ?
Details are at:

http://www.boost.org/doc/libs/1_41_0/libs/fusion/doc/html/fusion/functional/concepts/poly.html
http://www.boost.org/doc/libs/1_41_0/libs/fusion/doc/html/fusion/functional/concepts/def_callable.html
http://www.boost.org/doc/libs/1_41_0/libs/utility/utility.htm#result_of

basically, you're building a polymorphic function object, ie a functor
with templated arguments/return type.
What you can do is have one template operator() and use the result_of
protocol to validate the call signature
w/r to your T1,..,Tn. Then you can use tag-dispatchign to jump into the
proper internal implementation

class G
{
  template<class Sig> struct result;

  template<class This> struct result<This(T2,T3)> { typedef double type; };
  template<class This> struct result<This(T1,T1)> { typedef double type; };
  template<class This> struct result<This(T3,T1)> { typedef double type; };

  template<class A0,class A1>
  typename result<G(A0,A1)>::type
  operator()( A0 const& a0, A1 const& a1)
 {
    return eval(a0,a, mpl::pair<A0,A1>());
 }

  template<class A0,class A1>
  typename result<G(A0,A1)>::type
  eval( A0 const& a0, A1 const& a1, mpl::pair<T2,T3> const&) { /* ... */ }

  template<class A0,class A1>
  typename result<G(A0,A1)>::type
  eval( A0 const& a0, A1 const& a1, mpl::pair<T1,T1> const&) { /* ... */ }

  template<class A0,class A1>
  typename result<G(A0,A1)>::type
  eval( A0 const& a0, A1 const& a1, mpl::pair<T3,T1> const&) { /* ... */ }
};

or something like that.

-- 
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35

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