I am trying to write code that calls the class method specified as a template parameter. To simplify, you can assume that the method has a single parameter (of an arbitrary type) and returns void. The goal is to avoid the pattern in the calling site without typing the parameter type. Here is a sample code:
template <class Method> class WrapMethod {
public:
template <class Object>
Param* getParam() { return ¶m_; }
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 this method without writing the parameter type.
Foo foo;
WrapMethod<BOOST_TYPEOF(&Foo::Bar)> foo_bar;
foo_bar.GetParam()->FillWithSomething();
foo_bar.Run(foo);
So this code works, and that is almost what I want. The only problem is that I want to get rid of the BOOST_TYPEOF macro on the calling site. I would like to write something like WrapMethod<Foo::Bar> foo_barinstead WrapMethod<BOOST_TYPEOF(&Foo::Bar)> foo_bar.
, , , ( WrapMethod - ) , typeof.
, , , .
, : typemame Param . , FillWithSomething WrapMethod ( ). Param Param, . , , , BOOST_TYPEOF ( WrapMethod , , api , ).
:
, . , - WrapMethod<&Foo::Bar>, , . , ( ), . , - typeof_literal<0>::type, int typeof_literal<&Foo::Bar>::type, void (Foo*::)(Param) . , BOOST_TYPEOF, declype , . , :
template <template<class T> T value> struct typeof_literal {
typedef decltype(T) type;
};
++ 0x, ( BOOST_AUTO), auto -:
template <class T> WrapMethod<T> GetWrapMethod(T) { return WrapMethod<T>(); }
auto foo_bar = GetWrapMethod(&Foo::Bar);