Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-03-01 12:50:41


Andy Little wrote:
> "Jonathan Turkanis":
>> Andy Little wrote:

>>> How does this play with boost::result_of. Is there any potential to
>>> extend result_of by this?
>>
>> result_of should already get the right answers for these types.
>
> #include <iostream>
> #include "boost/utility/result_of.hpp" // v 1.32.0
>
> // BTW following requires #include
> "boost/function_types/function_type_parameter.hpp"
> #include "boost/function_types/function_type_result.hpp"
>
> typedef int f(int);
> int main()
> {
> // fails (Vc7.1, gcc3.2)
> // typedef boost::result_of<f>::type result_type;
>
> //succeeds(Vc7.1, gcc3.2)
> typedef boost::function_types::function_type_result<
> f
> >::type result_type;
> std::cout << typeid(result_type).name() <<'\n';
> }

I was too quick to say that result_of works with all the types handled by
funtion_types. It works with function pointers, function references and member
function pointers, but not with pure function types. The reason is purely
syntactic: a function type is not a legal return type. Why does this matter?
Because the type expression passed to result_of must be of the form F(arg1, ...,
argN).

The correct way to use result_of with function pointers is as follows:

    #include <boost/static_assert.hpp>
    #include <boost/type_traits/is_same.hpp>
    #include <boost/utility/result_of.hpp>

    typedef int (*f)(int);
    int main()
    {
        typedef boost::result_of<f(int)>::type result_type;
        BOOST_STATIC_ASSERT((boost::is_same<result_type, int>::value));
    }

Jonathan


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