I am doing a switch statement over a char, and oftentimes the only difference between, say, 'A' and 'a' is certain static constants i have in a particular struct and a random numeric constant. Here is an example:
switch(someChar)
{
case 'A':
{
typedef structWithConstants<caseA, UPCASE> constantsT;
someStruct s;
s.bla = bla;
s.foo = getfoo7(rat+constantsT::rat);
s.other = getOther10(other + constantsT::other);
someFunctionBar(&s);
}
break;
case 'a':
{
typedef structWithConstants<caseA, LOWCASE> constantsT;
someStruct s;
s.bla = bla;
s.foo = getfoo3(rat+constantsT::rat);
s.other = getOther10(other + constantsT::other);
someFunctionBar(&s);
}
break;
}
so in the above, literally the only difference in terms of code is the constantsT which gets used and the 7 switched to a three…is there a way to simplify the repetitive code above a bit? Maybe collapsing some of the common behavior between both cases?
it is possible to template a function, declare a type for those ‘foo’-s, and then pass the casing template param to some caller function