Boost logo

Boost Users :

From: Peter Soetens (peter.soetens_at_[hidden])
Date: 2006-08-11 10:09:55


Hi,

I was looking for a way to convert a member function type:

bool (X::*)(int) or bool (X::)(int)

to the 'C' equivalent, without the class pointer:

bool (*)(int) or bool(int)

Now, I used this helper class:

// Converts R (X::)(Args) to R (Args)
template<class F>
class UnMember
{
    typedef boost::function_type_signature<F> member_signature;
    typedef typename boost::mpl::erase<typename member_signature::types,
                                       typename boost::mpl::next<typename
boost::mpl::begin<member_signature>::type>::type>::type non_member_signature;

public:
   typedef typename
boost::function_type<boost::plain_function,non_member_signature>::type type;
};

And use it like:

typedef UnMember<bool(X::)(int)>::type PlainFunctionType;

It gets even complexer when one wants to convert a member function type to a
function type which takes the member as first argument:

// Converts R (X::)(Args) to R (X::*, Args)
template<class F>
class MemberToFirstArg
{
    typedef boost::function_type_signature<F> member_signature;
    // remove the class from the arg list:
    typedef typename boost::mpl::erase<typename member_signature::types,
                                       typename boost::mpl::next<typename
boost::mpl::begin<member_signature>::type>::type>::type non_member_signature;
    // insert it as first argument.
    typedef typename boost::mpl::insert<non_member_signature,
                                        typename boost::mpl::next<typename
boost::mpl::begin<non_member_signature>::type>::type,
typename boost::add_pointer<typename boost::mpl::at<typename
member_signature::types,boost::mpl::int_<1> >::type>::type
>::type arg_signature;
public:
     typedef typename
boost::function_type<boost::plain_function,arg_signature>::type type;
};

Is there an easier way to do this ? Does a boost library have a mechanism to
convert member function types to non-member function types and vice versa ?

Peter

-- 
Peter Soetens -- FMTC -- <http://www.fmtc.be>

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net