mardi 25 avril 2017

Duplicate code using c++11

This is my first post on stack overflow :) . Feel free to tell me if there is anything wrong with my post.

I'm currently working on a project and I have the following issue.

I have a C++ method that I want to work in two different ways :

void MyFunction()
{
  foo();
  bar();
  foobar();
}

void MyFunctionWithABonus()
{
  foo();
  bar();
  doBonusStuff();
  foobar();
}

And I would like not to duplicate my code because the actual function is much longer. The issue is I must not under any circumstance add execution time to the programm when MyFunction is called instead of MyFunctionWithABonus. That is why I cannot just have a boolean parameter that I check with a C++ comparison.

My idea would have been to use C++ templates to virtually duplicate my code, but I can't think of a way of doing in which I don't have additional execution time and I don't have to duplicate the code.

I'm not an expert with templates so I may be missing something.

Does any of you have an idea? Or is that just impossible in C++11? Thanks for your input! :)

Aucun commentaire:

Enregistrer un commentaire