BЈовић
BЈовић

Reputation: 64223

How to emulate strongly typed enum in C?

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;
}

Upvotes: 19

Views: 5531

Answers (3)

Nicolas Bachschmidt
Nicolas Bachschmidt

Reputation: 6505

As C doesn't provide namespaces, you can use prefixes instead.

enum MyEnum {
    MyEnumA = 1,
    MyEnumB,
    MyEnumC
};

enum OtherEnum {
    OtherEnumA = 1,
    OtherEnumB
};

Then, for conciseness in variable declarations, you may declare types for your enum, like this:

typedef enum MyEnum MyEnum;
typedef enum OtherEnum OtherEnum;

Finally, If you don't want to allow implicit conversions of OtherEnumB to the MyEnum type, Clang provides the -Wenum-conversion flag (I don't think there's a similar flag in GCC unfortunately).

/tmp/test.c:24:20: warning: implicit conversion from enumeration type 'enum OtherEnum' to different enumeration type 'MyEnum' (aka 'enum MyEnum') [-Wenum-conversion]
    MyEnum value = OtherEnumB;
           ~~~~~   ^~~~~~~~~~
1 warning generated.

This has the advantage to be simple, easy to understand and to work well with your (mine, at least) IDE's autocompletion.

Upvotes: 3

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

Here's my solution. Has a few advantages over @Eric's design:

  • Supports equality testing (e.g. A_VALUE_0 == value)
  • Doesn't rely on C99's compound literals
  • Can cast to assign an improper value to a enum.

Disadvantages:

  • Flags do not work (e.g. A_VALUE_0 | A_VALUE_1)
  • Cannot be switch'd
  • Can confuse the IDE on where the error lines are when testing for equality (e.g. A_VALUE_0 == B_VALUE_1)

Notes:

  • NEVER dereference a pointer of this type. Will cause crashes faster than a Lamborghini

Here's the implementation (compiled with -Werror & -pedantic):

typedef struct A { char empty[1]; } *A; // we use 'empty' so that we don't get a warning that empty structs are a GNU extension
#define A_VALUE_0 ((A) 0x1)
#define A_VALUE_1 ((A) 0x2)
#define A_VALUE_2 ((A) 0x4)

typedef struct B { char empty[1]; } *B;

#define B_VALUE_0 ((B) 0x0)
#define B_VALUE_1 ((B) 0x1)
#define B_VALUE_2 ((B) 0x2)

int main()
{
    A a = A_VALUE_0;

    int equal = (a == A_VALUE_1); // works!
    int euqal = (a == B_VALUE_1) // doesn't work

    A flags = A_VALUE_0 | A_VALUE_1; // doesn't work!

    switch (a) { // doesn't work
        case A_VALUE_0:
            puts("value 0");
            break;
        case A_VALUE_1:
            puts("value 1");
            break;
        case A_VALUE_2:
            puts("value 2");
            break;
        default:
            puts("unknown value");
            break;
    } // doesn't work

    // casting works for assignment:
    A b = (A) (B_VALUE_2);

    return 0;
}

Upvotes: 7

Eric Postpischil
Eric Postpischil

Reputation: 222942

You can do this:

// Declare A to use for an enumeration, and declare some values for it.
typedef struct { int i; } A;
#define A0  ((A) { 0 })
#define A1  ((A) { 1 })

// Declare B to use for an enumeration, and declare some values for it.
typedef struct { int i; } B;
#define B0  ((B) { 0 })
#define B1  ((B) { 1 })


void foo(void)
{
    // Initialize A.
    A a = A0;

    // Assign to A.
    a = A1;

    // Assign a value from B to A.
    a = B0; // Gets an error.
}

That gives you some typing, but it may be a nuisance, depending on what other operations you want to perform with the enumeration and its values.

Upvotes: 3

Related Questions