7

I have an enumeration called StackID, and throughout my code I have to static_cast it to int quite a bit - e.g.

StackID somestack;
int id = static_cast<int>(somestack);

Is there a shorthand alternative to doing this cast over and over again ? I have heard of "implicit" conversions - is that something I can use here?

(Possibly related to this question)

Community
  • 1
  • 1
BeeBand
  • 10,953
  • 19
  • 61
  • 83
  • 4
    What makes you think you need to cast them at all? They can stay enums for most uses of the enum. – rettops Aug 21 '10 at 17:54
  • The thing to do with an enum is to treat it as a real type of its own. Your code shouldn't be `int id` it should be `StackID id = somestack`. It should never be set by an int value, but only by the enum constants. – Zan Lynx Jul 10 '16 at 20:08

3 Answers3

12

Is there something you should use instead? Probably not. If you're doing enum casts to int I would question if you're using enums properly (or if you're having to interface with a legacy API.) That being said you don't have to static_cast enums to ints. That'll happen naturally.

See this article from MSN on enums and enum->int and int->enum (where you do have to use a static_cast.)

wheaties
  • 35,646
  • 15
  • 94
  • 131
  • 1
    Maybe clarify "that'll happen naturally"? To be specific, you can replace `int id = static_cast(somestack);` with `int id = somestack;`, if you want to. Someone reading the code is then less likely to realise that a conversion is taking place, but if your enums only involve a small range of values (less than MAX_INT), that probably doesn't matter. – Steve Jessop Aug 21 '10 at 16:30
  • @Steve Jessop: one could also introduce a static_assert to check that the `enum` doesn't spill over the boundaries. A comparison of relative sizes (with `sizeof`) could help tremendously. – Matthieu M. Aug 21 '10 at 18:01
  • 1
    aren't enums supposed to be an integral type anyways so the static_cast is redundant? –  Aug 21 '10 at 18:06
  • @0A0D: http://stackoverflow.com/questions/3537966/is-there-an-alternative-to-using-static-castint-all-the-time/3538531#3538531 – sbi Aug 21 '10 at 19:03
  • @0A0D: the cast is redundant as far as the *compiler* is concerned, but implicit narrowing conversions are a little risky from the POV of code comprehensibility. There is almost certainly no situation when you *want* to lose the most significant bits of an enum when converting it to `int`, and casts draw attention to the conversion. Hence my comment that it doesn't matter for small enums but might matter for big ones. It's better if you can switch on compiler warnings for implicit narrowing conversions, but with an existing code base sometimes that takes a bit of work. – Steve Jessop Aug 21 '10 at 19:11
  • 1
    @Steve: Enums over 4 billion? Sounds excessive. –  Aug 21 '10 at 20:39
  • @0A0D: it can happen, although obviously when you're using the enum as a flagset. Not so much if you're actually naming all the values. – Steve Jessop Aug 21 '10 at 21:58
  • @Steve: OTOH, a compiler might warn if, through porting, an implicit cast becomes narrowing that wasn't narrowing before. If you cast explicitly, it likely won't. – sbi Aug 21 '10 at 22:17
  • @sbi: absolutely: "it's better if you can switch on compiler warnings". – Steve Jessop Aug 22 '10 at 15:01
1

Enum values will start from zero by default and keep on increasing. Basically, enum constants themselves are integer constants. No need of typecasting them to int explicitly. When we want to represent multiple constants like error codes with unique values, instead of #define statements, we can make use of Enum.

bjskishore123
  • 6,144
  • 9
  • 44
  • 66
1

Is there a shorthand alternative to doing this cast over and over again ?

Well, wouldn't you know., it's your lucky day! Because, yes, there is a simpler way:

int id = somestack;

Any enum value is implicitly convertible into an int.

Anyway, from your two questions regarding this issue, I'll join the concerned voices asking whether an enum is really what you want here. (I'm not saying it's wring, I know too little about your problem to know that. But from what I know it seems questionable.)

sbi
  • 219,715
  • 46
  • 258
  • 445