I have a mixture of C++ classes, some of which store a state variable 0..9 as integer, others use '0' ... '9'. Currently I do:
enum { kOne = '1' };
class StoresValueAsInt {
static int value; // contains 0 ... 9
};
class StoresValueAsChar {
static char value; // contains '0' ... '9'
};
class StoresValueAsChar {
static char value;
};
template <typename X>
isOne() { return X::value == kOne; }
This allows me to write the buggy code isOne<StoresValueAsInt::value>(). Instead, I would like to have the compiler complain about this incorrect usage. I tried the following:
enum ValueInt {
kOne = 1
};
enum ValueChar {
kOne = '1'
};
class StoresValueAsInt {
static ValueInt value;
};
class StoresValueAsChar {
static ValueChar value;
};
class StoresValueAsChar2 {
static ValueChar value;
};
However, this does not work, because kOne is visible at the namespace level, and thus it has conflicting declarations of kOne.
Is there a way to not have the enums declared in the namespace? Or a better approach here?
updated: Added what I currently do; hoping to clarify my use case.
charand anintbecause acharis anunsigned int(orunsigned shorton some systems)