Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ how to simplify multiple functions with same logic but different parameter type?

void functionA(A a){
  for(auto x: vector_of_x) {
    // duplicate code, which cannot be moved to a common function
    x.abc(a);
    // some more duplicate code, which cannot be moved to a common function
  }
}

void functionB(B b){
  for(auto x: vector_of_x) {
    // duplicate code, which cannot be moved to a common function
    x.def(b);
    // some more duplicate code, which cannot be moved to a common function
  }
}

The duplicate code in functionA and functionB is quite a lot, so I’m looking to remove duplicate code. What’s the best way to achieve it?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can merge functionA and functionB into a function template, where it’s customizable whether abc or def gets called.

template <auto MemberFunction, typename T>
void function(T t){
  for(auto x: vector_of_x) {
    // duplicate code, which cannot be moved to a common function
    (x.*MemberFunction)(t);
    // some more duplicate code, which cannot be moved to a common function
  }
}

void functionA(A a){
  function<&A::abc>(a);
}

void functionB(B b){
  function<&B::def>(b);
}

However, member function pointers are a bit tricky syntax-wise, and they aren’t very flexible.
For example, what if you wanted to also debug-print something instead of just calling A::abc?
Therefore, you would use lambda expressions (or any other invocable object) in most cases:

template <typename F>
void function(F f){
  for(auto x: vector_of_x) {
    // duplicate code, which cannot be moved to a common function
    std::invoke(f, x); // or x(f), if that's sufficient
    // some more duplicate code, which cannot be moved to a common function
  }
}

void functionA(A a){
  function( [&a](X& x) { x.abc(a); } );
}

void functionB(B b){
  function( [&b](X& x) { x.def(b); } );
}

The latter solution is very idiomatic; it’s also what algorithms such as std::for_each in the standard library do.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading