I have the following code inside a C++ class:
class Features
{
#define Feature_Size_A 12345
#define Feature_Size_B 45678
#define Feature_Size_C 78901
//#define Feature_Size_D 14725
const int Feature_Sum = 0
#ifdef Feature_Size_A
+ Feature_Size_A
#endif
#ifdef Feature_Size_B
+ Feature_Size_B
#endif
#ifdef Feature_Size_C
+ Feature_Size_C
#endif
#ifdef Feature_Size_D
+ Feature_Size_D
#endif
;
#ifdef Feature_Size_A
static float Feature_A[Feature_Size_A];
#endif
#ifdef Feature_Size_B
static float Feature_B[Feature_Size_B];
#endif
#ifdef Feature_Size_C
static float Feature_C[Feature_Size_C];
#endif
#ifdef Feature_Size_D
static float Feature_D[Feature_Size_D];
#endif
};
I used to comment out features, like line 4, to compile and run different tests. But now I’d like to have the class as a template, so I can instantiate several versions with different features turned on or off in the same program.
I’m thinking of something like this:
template <bool Feature_A, bool Feature_B, bool Feature_C, bool Feature_D>
class Features
{
...
};
Features<true, true, true, false> f;
I tried with boost::mpl:vector’s but I’m struggling harshly.
BTW: This is not the complete code. The original code has 25 features.
I’m thankful for every idea not involving macros 🙂
Type lists can be used to solve this problem.
This can also be used to access any feature, regardless of what other features are or aren’t in the list.
Edit: I nubbed up some of the details, like not defining the array and trying to have “3features” as an identifier. Le fixed. Code compiles GCC 4.7.1.