Boost logo

Boost :

Subject: [boost] member function traits (was: Concept Traits Library)
From: Barend Gehrels (barend_at_[hidden])
Date: 2010-07-13 05:04:35


Hi,

Related to this message:

John Maddock wrote:
>> Even if the library doesn't provides an approach that works to define
>> all
>> the concepts, it has a lot of useful things. I also suggested to
>> Terje at
>> least to add the mpl and type traits extensions of the library.
>
> Agreed - in fact I don't see why we can't just add the new traits
> direct to the type_traits library?
>
> Certainly if someone would like to package these up as direct
> additions to type_traits (so I don't have to do much!) then I'd
> certainly look favorably on adding them.
>

and especially related to the function_traits of the type_traits
library. Is there any interest in class member function introspection?

According to the current documentation, "function_traits is intended to
introspect only C++ functions of the form R (), R( A1 ), R ( A1, ...
etc. ) and not function pointers or class member functions.".

We have, in our work on Boost.Geometry, the need to have the
function_traits also working on class member functions. We first had
defined a specific meta-function, only for the purpose of
concept-checking. Bruno suggested that using function_traits we can
avoid that. So we need it to work on member functions.

I did some experiments and it is possible to let function_traits
checking member functions as well, and it is easy to incorporate into
function_traits.

It can then be called, for a member function called "apply" of a
(template)class called Strategy, like this:

typedef typename boost::function_traits<typename
BOOST_TYPEOF(&Strategy::apply)>::arg1_type ptype1;

for the first argument (in our case a point type, which is then used in
the concept-checking using BCCL, to check if it fulfills the point
concept and to feed into this same apply method).

Adding member function support only needs two relatively simple additions:
1) a specialization for member functions, like this (here only for a
const function with 2 parameters)
template <typename Class, typename R, typename T1, typename T2>
struct function_traits_helper<R (Class::*) (T1, T2) const>
{
    BOOST_STATIC_CONSTANT(unsigned, arity = 2);
    typedef R result_type;
    typedef T1 arg1_type;
    typedef T2 arg2_type;
};

2) the boost::add_pointer call in function_traits should be avoided for
member functions, this can be solved by using a meta-function there:
template<typename Function>
struct function_traits : public detail::function_traits_helper
        <
            typename mpl::if_
                <
                    boost::is_member_function_pointer<Function>,
                    Function,
                    typename boost::add_pointer<Function>::type
>::type
>
{};

Experiments are here: http://codepad.org/Qw4Uf1aG

So I have a prototype, it seems to work fine and can easily be extended
and incorporated. Is there more interest in it, and do the type_traits
maintainers find this a good idea? Of is it already somewhere else
within Boost (or planned)?

Regards, Barend


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