Boost logo

Boost Users :

Subject: Re: [Boost-users] [Function] requiring an exact match in function signature?
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2010-04-04 01:49:54


>>> Anyway, if you want to test the signature of a function object, just
>>> take the address of &T::operator() and check it is what you want. Of
>>> course that won't work if it's a template or if it is overloaded.
>>
>> Could you be kind enough to give an example of how I can do such a check?
>
> This works on GCC, haven't tested with other compilers.
>
> [snip]

Thank you so much for this example code.
 
Not only does this allow me to enforce an exact match in the
function signature, it also allows me to avoid using boost::function
altogether (the only reason I was using it was in an attempt to
enforce the signature), thus avoiding the associated performance
overhead.
 
I also learned a lot about templates and metaprogramming while trying
to understand how it works!
 
I modified the code slightly to allow for both functors and plain
functions. As this is my first attempt at nontrivial metaprogramming,
I would very much appreciate any comments on my modifications:
 
template<class X, class Prototype>
struct function_to_member
{
    typedef typename boost::function_types::result_type<Prototype>::type result;
    typedef typename boost::function_types::parameter_types<Prototype>::type args;
    typedef typename boost::mpl::push_front<args, X*>::type base;
    typedef typename boost::mpl::push_front<base, result>::type type;
};
 
template<class X, class Prototype>
struct build_member_type
{
    typedef typename function_to_member<X, Prototype>::type root;
    typedef typename boost::function_types::member_function_pointer<root>::type type;
};
 
template<class X, class Prototype>
struct build_const_member_type
{
    typedef typename function_to_member<X, Prototype>::type root;
    typedef typename boost::function_types::member_function_pointer<root,boost::function_types::const_qualified>::type type;
};
 
template <typename Prototype>
struct build_function
{
    typedef typename boost::function_types::result_type<Prototype>::type result;
    typedef typename boost::function_types::parameter_types<Prototype>::type args;
    typedef typename boost::mpl::push_front<args, result>::type type_sequence;
    typedef typename boost::function_types::function_type<type_sequence>::type function_type;
    typedef typename boost::function_types::function_pointer<type_sequence>::type function_pointer_type;
    typedef typename boost::function_types::function_reference<type_sequence>::type function_reference_type;
};
 
template<class T, typename Sig>
struct has_signature_functor
{
    typedef char not_found;
    struct found
    {
        char x[2];
    };
    
    template<typename X, typename build_member_type<X, Sig>::type V>
    struct member
    {
    };
    
    template<typename X, typename build_const_member_type<X, Sig>::type V>
    struct const_member
    {
    };
    
    template<typename X>
    static found test(member<X, &X::operator()>*);
    
    template<typename X>
    static found test(const_member<X, &X::operator()>*);
    
    template<typename X>
    static not_found test(...);
    
    static const bool value = (sizeof(found) == sizeof(test<T>(0)));
    
    typedef boost::mpl::bool_<value> type;
};
 
template <class T, typename Sig>
struct has_signature_function
{
    static const bool value =
            boost::type_traits::ice_or<boost::is_same<T, typename build_function<Sig>::function_type>::value,
                                       boost::is_same<T, typename build_function<Sig>::function_pointer_type>::value,
                                       boost::is_same<T, typename build_function<Sig>::function_reference_type>::value
>::value;
};

template <class T, typename Sig>
struct has_signature
{
    static const bool value = boost::type_traits::ice_or<has_signature_function<T, Sig>::value,
                                                         has_signature_functor<T, Sig>::value>::value;
};
 
Thanks again!
Nate.
_________________________________________________________________
Live connected. Get Hotmail & Messenger on your phone.
http://go.microsoft.com/?linkid=9724462


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