views:

89

answers:

4

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?

+1  A: 

I believe what you want is an equivalent to the "static if" command that exists in D language. I am afraid such a feature does not exist in C++.

Note that if parts of your code vary depending on the feature your request, these parts don't belong in the main function because they are not part of the bare algorithm. So the option to delegate such features in functions seems like a good one.

EDIT
If your #ifdef statements are used to do the same subtask differently, then defining subfunctions is the right thing to do. It will make your code more readable, not less.

If they are used for completely different actions, well, your code is already cluttered. Do something about it.

As for the performance issue you fear might appear, trust your compiler.

EDIT2
I forgot to mention the answer to the first part of your code : use the following trick to add or remove members depending on "feature".

namespace helper
{
  template<int feature>
  struct A;

  template<>
  struct A<0> { // add member variables for case 0 };

  template<>
  struct A<1> { // add member variables for case 1 };
}

template<int feature>
class A : private helper::A<feature>
{
  // ... functions here
};
Benoît
Yes, something similar to "static if" seems to be necessary. Maybe that's possible in this case with boost templates (like enable_if)? I'm afraid that functions would clutter the code very much, there would be nested calls, and the code would become quite artificial, unreadable and perhaps less optimized for speed.
Thomas
That's a nice idea with helper::A! I'll try to recode some #ifdefs with functions and your trick later and see if I like the result.
Thomas
A: 

The common solution, is just to use #ifdef I'm afraid. :-)

Frederik Slijkerman
A: 

I don't get the point of common code duplication. If u are using template parameter u are just replacing your #ifdef with if(WITH_FEATURE_X). Are u talking about code bloat by the compiler. Since you are trying to replace #ifdef I am assuming you would be using either A<0> or A<1> at any point. So I don't see a code bloat even by the compiler.

aeh
+2  A: 

If you want to avoid duplicating the logic of function f you can use the template method pattern (no, not that kind of template.

template <bool enabled>
class helper {
protected:
    void foo() { /* do nothing */ }
};

template <>
class helper<true> {
protected:
    Y y;
    void foo() { /* do something with y */ }
};

struct A : private helper<WITH_FEATURE_X> {
    void f() {
        // common stuff

        foo(); // optimized away when WITH_FEATURE_X is false

        // more common stuff
    }
};
Motti
This seems to be the same suggestion as Benoît's, but it's good to know the name of the pattern and to see how it is applied to functions. Thank you.
Thomas