1

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.

3
  • The compiler is not going to complain about a comparison between a char and an int because a char is an unsigned int (or unsigned short on some systems) Commented May 5, 2011 at 20:35
  • Yes, then compiler does not complain about the error in the first version. This is why I wanted to define enums to have a type check; and the compiler would consider ValueInt and ValueChar as being different... Commented May 6, 2011 at 2:29
  • Yes, I am using your solution -- as you have stated "this is ugly," so my initial hope was that there is a more elegant solution... Commented May 6, 2011 at 21:19

2 Answers 2

3

As far as best practice, I'm really not sure what you are trying to accomplish here so I can't point you in the right direction.

However, to answer your question directly: Placing the enums in seperate namespaces would solve your issue:

namespace IntVals {
   enum ValueInt {
      kOne = 1
   };
}
namespace CharVals {
   enum ValueChar {
      kOne = '1'
   };
}

class StoresValueAsInt {
   static ValueInt value;
};
class StoresValueAsChar {
   static ValueChar value;
};

Then you should be able to use the following:

StoresValueAsInt::value == CharVals::kOne

Though this is ugly, and I wish I understood better what you were going for to offer a more elegant solution than this.

Sign up to request clarification or add additional context in comments.

Comments

1

Is there a way to not have the enums declared in the namespace? Or a better approach here?

Yes, you can differentiate your enums as below:

struct ValueInt {
  enum {  // no need to have enum Name
    kOne = 1
  };
};
struct ValueChar {
  enum {  // no need to have enum Name
    kOne = '1'
  };
};

Usage:

ValueInt::kOne;
ValueChar::kOne;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.