Define Enum with strong-typedef CPP

185 Views Asked by At

While defining a network-message struct:

  1. Want to hold the Op field as an Enum (so to limit the allowed values).
  2. Need to keep all fields as unsigned, and the Op specifically to be uint32_t.

Is there a way to combine these requirements? something like: typedef enum Op : uint32_t {save = 100, retrieve = 101, delete = 200};

1

There are 1 best solutions below

0
On BEST ANSWER

"100" "101" "200

Well, I don't quite understand what these strings mean? Your question is not very clear to me.

It sounds like what you want is enum class (Scoped enumerations).

enum class Op : uint32_t
{
    _100 = 100,
    _101 = 101,
    _200 = 200
};

limit the allowed values

Yes.

Need to keep all fields as unsigned, and the Op specifically to be uint32_t.

Yes.

You should name these enumerators something meaningful, rather than the same as the value.


According to your edited post:

enum class Op : uint32_t
{
    save = 100,
    retrieve = 101,
    delete_ = 200
};