I have a bunch of functions which read completely identical except for one line of code, which is different depending on the type of input parameter.
Example:
void Func(std::vector<int> input)
{
DoSomethingGeneral1();
...
DoSomethingSpecialWithStdVector(input);
...
DoSomethingGeneral2();
}
void Func(int input)
{
DoSomethingGeneral1();
...
DoSomethingSpecialWithInt(input);
...
DoSomethingGeneral2();
}
void Func(std::string input)
{
DoSomethingGeneral1();
...
DoSomethingSpecialWithStdString(input);
...
DoSomethingGeneral2();
}
I wonder how I could avoid this duplication using a template-like mechanism. If I understand “specialization” correctly, it does not avoid to have the code of the specialized functions twice?
here you go..
changed the parameters to references to avoid copies + assure you can use the changed values again in Func()