A possible solution is using lambda:
template<typename T>
static function doAlg(T fct)
{
// Common code, use fct() instead of f(a,b) or f(b)
}
void alg(A &a, B*b)
{
doAlg([&a, b](){ f(a,b); }); // when doAlg call fct(), it will call f(a,b);
}
void alg(B*b)
{
doAlg([b](){ f(b); }); // when doAlg call fct(), it will call f(b);
}
If you can not use feature of C++11 like lambda, just use functor or abstract class in place of lambda
Version with functor (if you can not/don't want to use lambda):
struct CallFWithAB
{
CallFWithAB(A &a, B *b):
_a(a), _b(b)
{}
A &_a;
B *_b;
void operator()()
{
f(_a,_b);
}
}
struct CallFWithB
{
CallFWithAB(B *b):
_b(b)
{}
B *_b;
void operator()()
{
f(_b);
}
}
template<class T>
static function doAlg(T& fct)
{
// Common code, use fct() instead of f(a,b) or f(b)
}
void alg(A &a, B*b)
{
CallFWithAB caller(a,b);
doAlg(caller); // when doAlg call fct(), it will call f(a,b);
}
void alg(B*b)
{
CallFWithB caller(b);
doAlg(caller); // when doAlg call fct(), it will call f(b);
}