开发者

How to pass a method pointer as a template parameter

I am trying to write a code that calls a class method given as template parameter. To simplify, you can suppose the method has a single parameter (of an arbitrary type) and returns void. The goal is to avoid boilerplate in the calling site by not typing the parameter typ开发者_StackOverflow中文版e. Here is a code sample:

template <class Method> class WrapMethod {
  public:
   template <class Object>
   Param* getParam() { return &param_; }
   Run(Object* obj) { (object->*method_)(param_); }
  private:
    typedef typename boost::mpl::at_c<boost::function_types::parameter_types<Method>, 1>::type Param;
    Method method_;
    Param param_
};

Now, in the calling site, I can use the method without ever writing the type of the parameter.

Foo foo;
WrapMethod<BOOST_TYPEOF(&Foo::Bar)> foo_bar;
foo_bar.GetParam()->FillWithSomething();
foo_bar.Run(foo);

So, this code works, and is almost what I want. The only problem is that I want to get rid of the BOOST_TYPEOF macro call in the calling site. I would like to be able to write something like WrapMethod<Foo::Bar> foo_bar instead of WrapMethod<BOOST_TYPEOF(&Foo::Bar)> foo_bar.

I suspect this is not possible, since there is no way of referring to a method signature other than using the method signature itself (which is a variable for WrapMethod, and something pretty large to type at the calling site) or getting the method pointer and then doing typeof.

Any hints on how to fix these or different approaches on how to avoid typing the parameter type in the calling site are appreciated.

Just to clarify my needs: the solution must not have the typename Param in the calling site. Also, it cannot call FillWithSomething from inside WrapMethod (or similar). Because that method name can change from Param type to Param type, it needs to live in the calling site. The solution I gave satisfies both these constraints, but needs the ugly BOOST_TYPEOF in the calling site (using it inside WrapMethod or other indirection would be fine since that is code my api users won't see as long as it is correct).

Response:

As far as I can say, there is no possible solution. This boil down to the fact that is impossible to write something like WrapMethod<&Foo::Bar>, if the signature of Bar is not known in advance, even though only the cardinality is necessary. More generally, you can't have template parameters that take values (not types) if the type is not fixed. For example, it is impossible to write something like typeof_literal<0>::type which evalutes to int and typeof_literal<&Foo::Bar>::type, which would evaluate to void (Foo*::)(Param) in my example. Notice that neither BOOST_TYPEOF or decltype would help because they need to live in the caling site and can't be buried deeper in the code. The legitimate but invalid syntax below would solve the problem:

template <template<class T> T value> struct typeof_literal {
  typedef decltype(T) type; 
};

In C++0x, as pointed in the selected response (and in others using BOOST_AUTO), one can use the auto keyword to achieve the same goal in a different way:

template <class T> WrapMethod<T> GetWrapMethod(T) { return WrapMethod<T>(); }
auto foo_bar = GetWrapMethod(&Foo::Bar);


Write it as:

template <typename Object, typename Param, void (Object::*F)(Param)> 
class WrapMethod { 
public: 
   Param* getParam() { return &param_; } 
   void Run(Object* obj) { (obj->*F)(param_); } 

private: 
    Param param_;
}; 

and

Foo foo; 
WrapMethod<Foo, Param, &Foo::Bar> foo_bar;       
foo_bar.getParam()->FillWithSomething();
foo_bar.Run(foo);

EDIT: Showing a template function allowing to do the same thing without any special template wrappers:

template <typename Foo, typename Param>
void call(Foo& obj, void (Foo::*f)(Param))
{
    Param param;
    param.FillWithSomthing();
    obj.*f(param);
}

and use it as:

Foo foo;
call(foo, &Foo::Bar);

2nd EDIT: Modifying the template function to take the initialization function as a parameter as well:

template <typename Foo, typename Param>
void call(Foo& obj, void (Foo::*f)(Param), void (Param::*init)())
{
    Param param;
    param.*init();
    obj.*f(param);
}

and use it as:

Foo foo;
call(foo, &Foo::Bar, &Param::FillWithSomething);


If your compiler supports decltype, use decltype:

WrapMethod<decltype(&Foo::Bar)> foo_bar;

EDIT: or, if you really want to save typing and have a C++0x compliant compiler:

template <class T> WrapMethod<T> GetWrapMethod(T) { return WrapMethod<T>(); }
auto foo_bar= GetWrapMethod(&Foo::Bar);

EDIT2: Although, really, if you want it to look pretty you either have to expose users to the intricacies of the C++ language or wrap it yourself in a preprocessor macro:

#define WrapMethodBlah(func) WrapMethod<decltype(func)>


Have you considered using method templates?

 template <typename T> void method(T & param)
 {
   //body
 }

Now the compiler is able to implicitly determine parameter type

           int i;
           bool b;

           method(i);
           method(b);

Or you can provide type explicitly

           method<int>(i);

You can provide specializations for different data types

           template <> void method<int>(int param)
           {
               //body
           }


When you are already allowing BOOST_TYEPOF(), consider using BOOST_AUTO() with an object generator function to allow type deduction:

template<class Method> WrapMethod<Method> makeWrapMethod(Method mfp) {
    return WrapMethod<Method>(mfp);
}

BOOST_AUTO(foo_bar, makeWrapMethod(&Foo::Bar));


Okay let's have a go at this.

First of all, note that template parameter deduction is available (as noted in a couple of answers) with functions.

So, here is an implementation (sort of):

// WARNING: no virtual destructor, memory leaks, etc...

struct Foo
{
  void func(int e) { std::cout << e << std::endl; }
};

template <class Object>
struct Wrapper
{
  virtual void Run(Object& o) = 0;
};

template <class Object, class Param>
struct Wrap: Wrapper<Object>
{
  typedef void (Object::*member_function)(Param);

  Wrap(member_function func, Param param): mFunction(func), mParam(param) {}

  member_function mFunction;
  Param mParam;

  virtual void Run(Object& o) { (o.*mFunction)(mParam); }
};

template <class Object, class Param>
Wrap<Object,Param>* makeWrapper(void (Object::*func)(Param), Param p = Param())
{
  return new Wrap<Object,Param>(func, p);
}


int main(int argc, char* argv[])
{
  Foo foo;
  Wrap<Foo,int>* fooW = makeWrapper(&Foo::func);
  fooW->mParam = 1;
  fooW->Run(foo);

  Wrapper<Foo>* fooW2 = makeWrapper(&Foo::func, 1);
  fooW2->Run(foo);
  return 0;
}

I think that using a base class is the native C++ way of hiding information by type erasure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜