tags:

views:

160

answers:

3

Hi,

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)

+8  A: 

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
+1 on not using enums properly.
casablanca
Maybe clarify "that'll happen naturally"? To be specific, you can replace `int id = static_cast<int>(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
@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.
aren't enums supposed to be an integral type anyways so the static_cast is redundant?
0A0D
@0A0D: http://stackoverflow.com/questions/3537966/is-there-an-alternative-to-using-static-castint-all-the-time/3538531#3538531
sbi
@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
@Steve: Enums over 4 billion? Sounds excessive.
0A0D
@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
@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
@sbi: absolutely: "it's better if you can switch on compiler warnings".
Steve Jessop
A: 

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
+2  A: 

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