Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2008-04-29 06:40:21


David Abrahams <dave_at_[hidden]> writes:

> 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.
>
> Is there an easier way?

In cases like this, I've often just overloaded call():

    template<typename R>
    R call(R(*f)())
    {
        return f();
    }

If you've got function call arguments to deal with, it's not so easy, as you
can end up with a lot of overloads.

Anthony

-- 
Anthony Williams            | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

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