When I want to have a member function as an argument to a template, is there a way to build it without providing a type Caller?
struct Foo
{
template <typename Caller, void (Caller::*Func)(int)>
void call(Caller * c) { (c->*Func)(6); }
};
struct Bar
{
void start()
{
Foo f;
f.call<Bar, &Bar::printNumber>(this);
^^^^
}
void printNumber(int i) { std::cout << i; }
};
int main ()
{
Bar b;
b.start();
return 0;
}
when i try
template <void (Caller::*Func)(int), typename Caller>
void call(Caller * c) { (c->*Func)(6); }
and name it like
f.call<&Bar::printNumber>(this);
I get an error Caller is not class....
So, is there a way to let the compiler infer the type of Caller?
source
share