I want to put several method pointers in a container, so I implemented a simple std::function
- like template class:
template<typename ...Args>
class MethodHolder : MethodHolderBase
{
public:
typedef void(*MethodType)(Args...);
MethodHolder(MethodType method) : method(method) {}
virtual ~MethodHolder() {}
void Invoke(Args... args)
{ method(args...); }
private:
MethodType method;
};
It has a base class so I can write code like this:
void func(int);
...
std::vector<MethodHolderBase*> funcs;
funcs.push_back(new MethodHolder<int>(&func));
...
Then I add a wrapper to call Invoke()
from MethodHolderBase
:
class Method
{
public:
...//constructors, assignment operator, destructor
template<typename ...Args>
void Invoke(Args&&... args)
{
auto pHolder = dynamic_cast<MethodHolder<Args...>*>(pBase);
if (pHolder)
pHolder->Invoke(args...);
}
private:
MethodHolderBase* pBase;
}
Now, I can put some instances of Method in a container, and call Invoke()
to call a function. It worked. But once I changed the declaration of func
to:
void func(int&&);
It seems like no matter which type I pass to Method::Invoke()
, a int&&
will not be deduced, so the dynamic_cast
fails.
Should I make some amendment to Method::Invoke
? If so, How? Or there is a different solution?