AlwaysLearning - 1 year ago 58
C++ Question

# Factoring out an overloaded function call

Suppose we have two overloads of a function, where

`A`
and
`B`
are user-defined types:

``````void f(A &a, B *b);
void f(B *b);
``````

We also have the corresponding overloads of a function implementing an algorithm:

``````void alg(A &a, B *b) {
// ... some code calling f(a,b)
}

void alg(B *b) {
// ... same code as alg(A &a, B *b), with f(a,b) replaced by f(b)
}
``````

Is there a way to factor out the calls to
`f`
to avoid code duplication in this case?

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);
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download