If the convention is not applicable then screw the convention. Seriously, conventions are not to be followed blindly. In this case the best the convention can do for you is to remind you that you are doing something which is uncommon in your work environment and that you should add a comment explaining why you decided to not follow the convention. Declare the helper outside of the function if you think this is the right thing to do.
However, as foo
is not templated, I do assume that the types the Helper
has to handle is from a limited set of types and that overloads would do as well:
void foo() {
struct MyHelper {
void func(TypeA, int x, int y) {
some_other<TypeA>(...);
}
void func(TypeB, int x, int y) {}
void func(TypeC, int x, int y) {}
};
MyHelper helper;
if (some_logic) {
helper.func(TypeA{},x, y);
} else if (some_other) {
helper.func(TypeB{},x, y);
// ....
}
If TypeA
is nothing you want to create instances of on the fly, you could use tags instead to pick the right overload. However, from your usage it looks like you neither need templates nor overloads, but simple named functions would be fine:
void foo() {
struct MyHelper {
void funcA(int x, int y) {
some_other<TypeA>(...);
}
void funcB(int x, int y) {
some_other<TypeB>(...);
}
void funcC(int x, int y) {}
};
MyHelper helper;
if (some_logic) {
helper.funcA(x, y);
} else if (some_other) {
helper.funcB(x, y);
// ....
}
Templates and overloads enable great flexibility, but sometimes you just don't need that flexibility and simply using differently named functions is the much simpler alternative.