2

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?

Emiliano
  • 22,232
  • 11
  • 45
  • 59
  • You mean, you want to know how [`function_traits`](http://www.boost.org/doc/libs/1_43_0/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html) is implemented? – kennytm Jul 16 '10 at 07:42
  • @Kenny: Total side note, but I just spent forever working on my own `function_traits` and was just finishing, and now I found out Boost has it. Should of known. .-. – GManNickG Jul 16 '10 at 07:49
  • @GMan as a total side side note, I thought that it's programmers who would have a finely tuned sense for grammatically sound structures, given the years we spend telling compilers what we want. `s/Should of/Should have/g`. Argh! Sorry, could not help it! ;) – Alex B Jul 16 '10 at 07:58
  • @Alex: But there isn't a [compiler for English](http://stackoverflow.com/questions/282329/what-are-five-things-you-hate-about-your-favorite-language/282394#282394). – kennytm Jul 16 '10 at 08:06
  • @Alex: Ha, true true. :) – GManNickG Jul 16 '10 at 08:06
  • @Alex, @GMan If G.W.Bush can do it, so can GMan. http://www.slate.com/id/2071155 – cape1232 Jul 16 '10 at 15:58

1 Answers1

6

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
  • 97,545
  • 26
  • 194
  • 236