Boost logo

Boost :

From: shunsuke (pstade.mb_at_[hidden])
Date: 2008-04-30 00:04:34


David Abrahams wrote:
> Now I just ran into another issue, from the other side of the result_of
> interface. If I have a function template taking a function object
> argument by reference, and I want to use result_of with it, I need to do
> something special to account for the case where I've been passed a
> function reference. To wit:
>
> template <class F>
> typename result_of<F()>::type
> call(F const& f)
> {
> return f();
> }
>
> int f() { return 0; }
>
> int x = call(f);
>
> The problem is that in the above case, F is deduced as int(), and we
> form an illegal function type (one that itself returns a function type)
> in result_of<F()>::type.

AFAIK, there is another defect. This doesn't compile:

   template<class F>
   typename result_of<typename result_of<F()>::type()>::type
   call_call(F f)
   {
       return f()();
   }

if `f()` returns a lvalue FunctionObject.

> To deal with this I need something more like
>
>
> template <class F>
> typename result_of<
> typename mpl::if_<
> is_class<F>
> , F
> , typename add_pointer<F>::type
> >::type()
> >::type
> call(F const& f)
> {
> return f();
> }
>
> Is there an easier way?

How about `boost::decay`?
Or an "imaginary" function `apply` might be better.

   template<class F>
   typename result_of<typeof_apply(F const &)>::type
   call(F const &f)
   {
       return apply(f);
   }

Regards,

-- 
Shunsuke Sogame

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk