tags:

views:

114

answers:

5

I had code that looked like this:

enum EEventID {
  eEvent1,
  eEvent2,
  ...
  eEventN };

And that got reviewed and changed to

typedef enum {
  eEvent1,
  eEvent2,
  ...
  eEventN } EEventID;

What is the difference between the two? Why make the change? When I looked at this question, the only mention of typedefs got downvoted.

+2  A: 

I found one possible answer here: "Well it is relevent to C language in which if you use an enum or structure which are user defined data types, you need to specify theenum or struct keyword while using it."

Maybe your reviewer is a hardcore C coder, and he does it automatically, out of habit.

Alex Emelianov
+11  A: 

The two are identical in C++, but they're not the same in C -- in C if you use the typedef you get code that is compatible between C and C++ (so can be used freely in a header file that might be used for either C or C++). That's the only reason I can see for preferring it.

Chris Dodd
Thanks for the elucicdation. I was beginning to worry if there was yet another corner about C++ that I thought I knew but was wholly ignorant of (see: the template gymnastics that the guys in Boost do so easily)
mmr
"The two are identical in C++" - worth pointing out that on some implementation debug information may differ as the typedef is effectively to an anonymous or randomly named enum, so debug info may document it as such rather than as "EEventID". This leads some people to the even uglier `typedef enum Idn1 {...} Idn2;`, with one typically formed by adding a prefix or suffix to the other :-( - it doesn't exactly solve the problem, but provides some more traceable idn.
Tony
A: 

Your reviewer doesn't know his C from his C++.

Kevin
+2  A: 

If there's a standard at the company, it could just be for consistency. Even if you never use in C, it's still better to do it the same way everywhere.

Lou Franco
+2  A: 

The typedef is superfluous in C++.
In C it is necessary if you want to define a variable of your enum type like so: EEventID event;, otherwise you would've had to specify it's an enum: enum EEventID event;.

Eugen Constantin Dinca