I have a lot of code like this:
#define WITH_FEATURE_X
struct A {
#ifdef WITH_FEATURE_X
// ... declare some variables Y
#endif
void f ();
};
void A::f () {
// ... do something
#ifdef WITH_FEATURE_X
// ... do something and use Y
#else
// ... do something else
#endif
// ... do something
}
and I'd like to replace the #defines with template parameters:
template < int WITH_FEATURE_X > // can be 0 or 1
struct A;
But I don't want to duplicate almost the entire code of A::f() for A<0>::f() and A<1>::f() just for the few lines that depend on the parameter. I also don't want to call functions instead of the previous #ifdefs. What is the common solution?