How to emulate strongly typed enum in C?

Viewed 4029

In C++03, it is possible to emulate strongly typed enum by putting it in a class (or a namespace ) :

struct MyEnum
{
  enum enumName
  {
    VALUE_1 = 1,
    VALUE_2,
  };
};

and to use it :

MyEnum::enumName v = MyEnum::VALUE_1;

Is it possible to do something similar in C? If yes, how?


I tried like this, but off course that doesn't work :

struct A
{
  enum aa
  {
    V1 = 5
  };
};

int main()
{
  A::aa a1 = A::V1;
  enum A::aa a2 = A::V1;
  struct A::aa a3 = A::V1;

  return 0;
}
3 Answers
Related