1

Is it in any way safer/"better" to do:

enum ENUM { // can be enum class too
...
};
int incoming_value_from_other_process;
auto e = static_cast<ENUM>(incoming_value_from_other_process);

than

auto e = ENUM(incoming_value_from_other_process); // more compact and readable, faster to type

EDIT : this is for intra-process and/or networking communication where enums are just serialized in messages and then directly converted.

Bill Kotsias
  • 3,258
  • 6
  • 33
  • 60
  • 1
    Ideally you shouldn't be doing either since you have no guarantee that the value you are specifying is a valid enum value. – NathanOliver Mar 09 '20 at 13:03
  • 2
    With just the tiny example you have given us, I don't see the point in any of them. Why not just assign the relevant enum value ? Not doing it defeats the purpose of the enums' static types. The only use-case in which imo it is relevant to static_cast from an int is when you are doing networking/intraprocess communication – Xatyrian Mar 09 '20 at 13:03
  • 2
    Does this answer your question? [What is the difference between static\_cast<> and C style casting?](https://stackoverflow.com/questions/1609163/what-is-the-difference-between-static-cast-and-c-style-casting) – anatolyg Mar 09 '20 at 13:07
  • @Xatyrian -- it's legal and often useful to assign values to a variable of an enumerated type that do not correspond to the value of any of the enumerators. – Pete Becker Mar 09 '20 at 13:07
  • @PeteBecker I have no doubt that it is legal but it feels like a code smell to me. – Xatyrian Mar 09 '20 at 13:18
  • @Xatyrian It's for intra-process/networking communication – Bill Kotsias Mar 09 '20 at 13:18
  • The thing is, I thought that this is *not* c-style casting, but actually enum-value-initialization, like saying `auto i = int(5);` – Bill Kotsias Mar 09 '20 at 13:19
  • There's nothing wrong about C-style casts (as long as you don't cast to pointers/references). I'd use `auto e = ENUM(5);` (or `ENUM e = ENUM(5);`). – HolyBlackCat Mar 09 '20 at 13:22
  • 1
    @Xatyrian -- the standard goes out of its way to define the range of values that can be assigned to an enumerated type. Basically, you can use any of the bits that are needed for the various defined values. That makes it straightforward to write types that act like bit sets. `enum { input = 1, output = 2 };` can be used to represent something that does both input and output by assigning `input | output` (i.e., 3) to the value. That can be made easier to use by overloading the various bitwise operators that you need, so that they return a value of the enumerated type. – Pete Becker Mar 09 '20 at 13:23
  • 1
    @BillKotsias Yes, it's not a C-style cast, you are aboslutely correct. – anatolyg Mar 09 '20 at 13:25
  • @PeteBecker it perfectly makes sense for flags, i see – Xatyrian Mar 09 '20 at 13:29
  • 1
    @NathanOliver They're all valid enum values ;) #usualargumentsaboutwhatenumsreallymean – Asteroids With Wings Mar 09 '20 at 13:43

1 Answers1

2

Imho, direct initialization is like:

enum Value {
  kValue = 5,
};

Value e = kValue;

And there is disadvantage of static_cast and your initialization vs direct initialization. Initialization by 'kValue' protects you from nonexistent value, improves readability, etc.

I think, both cases are equal. But second case is more readable.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Evgeny
  • 1,072
  • 6
  • 6