tags:

views:

73

answers:

1

I'm trying to figure out how could many boost classes be able to accept a function signature as template argument and then "extract" from there the result type, first argument type and so on.

template <class Signature>
class myfunction_ptr
{
   Signature* fn_ptr;
public:
   typedef /*something*/  result_type;  // How can I define this?
   typedef /*something*/  arg1_type;  // And this?
};

I know that boost also provides a more portable implementation, using a template argument for each function argument and that's easy to understand. Can someone explain me the magic beyond this?

+5  A: 

At the core its just specialization and repetition:

template<class S> struct Sig;

template<class R> struct Sig<R ()> {
    typedef R result_type;
};

template<class R, class T0> struct Sig<R (T0)> {
    typedef R result_type;
    typedef T0 first_type;
};

// ...
Georg Fritzsche
Looking at function_traits.hpp I can totally see your point
happy_emi