Boost logo

Boost :

Subject: Re: [boost] [functional] Interested in can_be_called<F, Sig> metafunction?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2012-11-06 18:10:46


On 04/11/2012 20:54, Matt Calabrese wrote:
> On Sun, Nov 4, 2012 at 2:23 PM, Mathias Gaunard <
> mathias.gaunard_at_[hidden]> wrote:
>
>> Personally, I've found it more useful to just not define
>> result_of<F(args)>::type if F(args) cannot be called.
>>
>
> The only problem with that is that you have to rely on the creator of a
> function object to abide by this convention. "can_be_called" (callable
> might be a simpler name and is what was used with C++0x concepts) would
> work regardless of whether or not result_of is defined.

result_of does not have to be implemented in terms of
result<Sig>::type/result_type.

template<class T, class R = void>
struct enable_if_type
{
   typedef R type;
};

template<class Sig, class Enable = void>
struct result_of {};

template<class F, class... Args>
struct result_of<F(Args...)
                , typename enable_if_type<
                    decltype( std::declval<F>()
                              (std::declval<Args>()...)
                            )
>::type
>
{
   typedef decltype(std::declval<F>()(std::declval<Args>()...)) type;
};

Of course, if you don't have decltype, you can't do that and must use
the result_type or result templates.

In C++03, you'll be able to deduce whether the expression in valid, but
not what the result type is.


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