Boost logo

Boost :

Subject: Re: [boost] Checking for return types (result_of) - redux..
From: Edward Grace (ej.grace_at_[hidden])
Date: 2009-08-21 13:10:25


>> To make matters worse (rhetorical); if I have a class with a set of
>> different functors each with a different return type how can I make
>> sure the magic typedef (result_type) is simultaneously correct for
>> each?
>
> In this case, you would need to use the more complex
> result template.
>
> struct square {
> template<class Signature>
> struct result;
> template<class This, class T>
> struct result<This(T)> {
> typedef typename boost::remove_const<
> typename boost::remove_reference<T>::type
>> ::type type;
> };
> template<class T>
> T operator()(const T& t) const { return t * t; }
> };

Ouch! Crikey Batman!

>> So, my best solution -- I think -- is to make sure my
>> functions/functors are unary and of the form:
>>
>> void function_body(double return&);
>>
>> that should be fairly easy to check for in all cases. To users it
>> will
>> appears weird in the extreme of course.....
>
> If all you are doing is checking the return type,
> you are probly better off using Boost.ConceptCheck.

That does, conceptually, (pardon the pun) seem like a good idea.

While my above kludge will work, along with the use of
boost::function I'd rather get what I want -- a guarantee of the
return type being correct or a compile error, than effect that
outcome through some other means.

I'm not (at all) familiar with it's usage of Boost.ConceptCheck,
something like this perhaps?

template <class O>
BOOST_CONCEPT_REQUIRES(
  ((Generator)),
  (double)
)
void
class_scope<types...>::measure_execution_time(O f,.... etc...) {
   code goes here...
}

Here O is the abstract function object, it could be a member function
ordinary function, anything that can be invoked with

   f();

Since it takes no arguments it's presumably a model of 'Generator'
and I require it's return type to be 'double'.

Before I get knee deep in compiler errors, does that seem about right
to you?

Cheers for this,

-ed


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