I referred to this somewhat similar question. However here the scenario is different:
struct A
{
void foo (int i) {} // choice
void foo (double i) {}
};
template<typename ObjType, typename FuncPtr>
void ReceiveFuncPtr (ObjType o, FuncPtr pf)
{
(o.*pf)(1);
}
int main ()
{
A obj;
ReceiveFuncPtr(obj, &A::foo); // don't want typecast here
}
In the above test code, I have an overloaded foo
inside A
. Had there been only 1 foo
then the code works fine. But for overloading case, compiler complains as:
error: no matching function for call to `ReceiveFuncPtr(A&, [unresolved overloaded function type])'
Instead of explicit typecasting while calling ReceiveFuncPtr()
, is there any way that we can make some changes in its template
parameter and enable it to receive foo(int)
version always for any similar class A
?
Edit: Idea is not to worry about the type while calling the function. It should be as simple as, ReceiveFuncPtr(obj, &A::foo);
And let the template
do its work.