Boost logo

Boost :

Subject: Re: [boost] Checking for return type -- BOOST_STATIC_ASSERT?
From: Edward Grace (ej.grace_at_[hidden])
Date: 2009-08-21 04:01:46


On 20 Aug 2009, at 23:57, Scott McMurray wrote:

> 2009/8/20 Edward Grace <ej.grace_at_[hidden]>:
>>
>> if O is compatible with the concept of a functor and O returns a
>> type that
>> can be converted to a float, then we are good to go -- otherwise
>> I'd like an
>> intelligible error.
>>
>> If, for example, O is of the type 'void (*)()' I'd like it (the
>> compiler) to
>> spit out,
>> "The function pointer of type O must of signature float (*)()"
>>
>
> Sounds like you want something like the MPL_ASSERT example at [1]
> along with the result_of metafunction[2].
>
> [1] http://www.boost.org/doc/libs/1_39_0/libs/mpl/doc/refmanual/
> assert.html
>
> [2] http://www.boost.org/doc/libs/1_39_0/libs/utility/
> utility.htm#result_of
>
> HTH,
> ~ Scott

Thanks for that tip Scott. Knowing where to look is always half the
battle. I think I want to do something like the following (ignore
_TEMPLATE_ and _TIMER_, they just expand to the usual template and
scope for a template class).

     _TEMPLATE_
     template <class O1, class O2>
     void
     _TIMER_
     measure_percentage_speedup(O1 fa,
                               O2 fb,
                               double &MinPercent,
                               double &MedPercent,
                               double &MaxPercent) {
       // We want to make sure that the functions return something.
       // the following doesn't seem to work.
       typedef typename boost::result_of<O1()>::type result_of_fa;
       typedef typename boost::result_of<O2()>::type result_of_fb;
       BOOST_MPL_ASSERT(( boost::is_same<result_of_fa, unsigned> ));
       BOOST_MPL_ASSERT(( boost::is_same<result_of_fb, unsigned> ));
...
...

No joy --- I can stick in whatever function type I want, with
whatever return type (e.g. void) and the asserts don't complain.

Is the above the type of thing you had in mind or have I got the
wrong end of the stick?

For what it's worth, I get an error if returning void from the
function f() because of this subsequent line, in another part of the
code,

        // Time a set of function calls.
        t0 = chrono();
        for (counter n=0; n < iterations; ++n) test::live_code += f();
        t1 = chrono();

however I'd like to make this requirement of a non-void return
something more obvious.

Thanks,

-ed


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