views:

188

answers:

2

I want to be able to templatize a class on a member function without needing to repeat the arguments of the member function -- i e, derive them automatically. I know how to do this if I name the class based on how many arguments the function takes, but I want to derive that as well.

Something like this, although this doesn't work (at least in MSVC 2008 sp1, which is my target compiler):

class Foo {
  void func0();
  int func2(char *, float);
};

template<typename T> class Wrapper;

// specialize for zero-argument void func
template<typename Host, void (Host::*Func)()> class Wrapper<Func> : public Base {
  ... specialization goes here ...
};

// specialize for two-argument value func
template<typename Host, typename Ret, typename Arg0, typename Arg1, Ret (Host::*Func)(Arg0, Arg1)> class Wrapper<Func> : public Base {
  ... specialization goes here ...
};

Through "Base" I can then treat these polymorphically. In the end, I want to use this to create a simple wrapper syntax for a scripting language:

WrapClass<Bar> wrap(
  MemberFunction<&Bar::func0>("func0") +
  MemberFunction<&Bar::func2>("func2")
  );

However, that doesn't work: the specialization syntax is wrong, because you can't match a function pointer to a typename argument.

+1  A: 

The C++ standard library provides mem_fun_ref which sort of works how you want, though it only works for nullary and unary functions. Of course, you can use a struct with all the parameters as your one argument.

rlbond
Sadly, I can't use a struct, because that doesn't work with the use case of binding a scripting language to a bunch of existing classes in a simple way. Thanks for the suggestion, though!
Jon Watte
+1  A: 

I believe you'll need to take a traits approach, the most common library of which is boost's, but if you wanted to avoid boost, it wouldn't be extremely difficult to roll your own if you limited the scope of the implementation to just pointer-to-member-functions and the traits on those you need (modern c++ design is a great book explaining the theory). Here's how I would do it with boost's function_traits and enable_if.

You could use a generic template argument, enable_if it for function pointers, then use function types (or type traits) to pull out out the information you need:

#include <boost/function_types/function_arity.hpp>
#include <boost/function_types/is_member_pointer.hpp>

template<typename T, class Enable = void> class Wrapper;

/* other specializations... */

// For member functions:
template <class T>
class Wrapper<T, typename enable_if<is_member_pointer<T> >::type>
{ /* function_arity<T>::value has the number of arguments */ };

See this and this

Todd Gardner
Didn't I mention the "no boost" requirement? I guess I didn't.I do have such a requirement, though -- sorry I didn't mention it. However, I'll dive into the boost headers and look at what they did, and mimic the small subset that I need.I believe I'll end up having to define an inner template of Wrapper that is specialized on arity in turn.
Jon Watte
You did in the title. However, my point is; to do this, you need a traits library, and the only one I know of is boost. If you want to do it without boost, you'll have to roll your own or find another.
Todd Gardner