Boost logo

Boost :

Subject: Re: [boost] Checking for return types (result_of) - redux..
From: Jeffrey Hellrung (jhellrung_at_[hidden])
Date: 2009-08-21 15:01:00


Edward Grace wrote:
>> 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

http://www.boost.org/doc/libs/1_39_0/libs/concept_check/reference.htm#function-object-concepts

I use BOOST_CONCEPT_ASSERT in my own code:

template< ... >
template< class O >
void class_scope< ... >::measure_execution_time(O f, ...)
{
     // Assert that, e.g.,
     // double x = f();
     // is okay.
     BOOST_CONCEPT_ASSERT((boost::Generator< O, double >));
     ...
}

I expect the following (or something like it) is what you'd do if you
prefer BOOST_CONCEPT_REQUIRES, but as alluded to above, I've never
actually used BOOST_CONCEPT_REQUIRES.

template< ... >
template< class O >
BOOST_CONCEPT_REQUIRES(
     ((boost::Generator< O, double >)),
     void
) class_scope< ... > ::measure_execution_time(O f, ...)
{
     ...
}

- Jeff


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