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
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::type base;
typedef typename boost::mpl::push_front::type type;
};
template
struct build_member_type
{
typedef typename function_to_member::type root;
typedef typename boost::function_types::member_function_pointer<root>::type type;
};
template
struct build_const_member_type
{
typedef typename function_to_member::type root;
typedef typename boost::function_types::member_function_pointer::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::type type_sequence;
typedef typename boost::function_types::function_type::type function_type;
typedef typename boost::function_types::function_pointer::type function_pointer_type;
typedef typename boost::function_types::function_reference::type function_reference_type;
};
template
struct has_signature_functor
{
typedef char not_found;
struct found
{
char x[2];
};
template::type V>
struct member
{
};
template::type V>
struct const_member
{
};
template<typename X>
static found test(member*);
template<typename X>
static found test(const_member*);
template<typename X>
static not_found test(...);
static const bool value = (sizeof(found) == sizeof(test<T>(0)));
typedef boost::mpl::bool_<value> type;
};
template
struct has_signature_function
{
static const bool value =
boost::type_traits::ice_or::value,
boost::is_same::value,
boost::is_same::value
>::value;
};
template
struct has_signature
{
static const bool value = boost::type_traits::ice_or::value,
has_signature_functor::value>::value;
};
Thanks again!
Nate.
_________________________________________________________________
Live connected. Get Hotmail & Messenger on your phone.
http://go.microsoft.com/?linkid=9724462