tags:

views:

3376

answers:

5

I haven't written any C++ in years and now I'm trying to get back into it. I then ran across this and thought about giving up:

typedef enum TokenType
{
    blah1   = 0x00000000,
    blah2   = 0X01000000,
    blah3   = 0X02000000
} TokenType;

What the heck is this? Why is the typedef keyword used here? Why does the name TokenType appear twice in this declaration? How are the semantics different from this:

enum TokenType { blah1 = 0x00000000, blah2=0x01000000, blah3=0x02000000 };

Thanks!

+5  A: 

Isn't this a 'C' style enum?

Rob
Yes, it is C style, and allowed in C++ for backwards compatibility even if the typedef is unnecessary
David Rodríguez - dribeas
yeah c++ says "In a given scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers."
Johannes Schaub - litb
+5  A: 

Holdover from C.

Tim
Dunno that the 'early' qualifier is relevant; you would still write that in C if you wanted to use the type name without the enum prefix.
Jonathan Leffler
true. I will delete it. I have not followed the C spec for a long long time. I was too lazy to check the c/c++ distinction... -1 for me.
Tim
+9  A: 

In C, declaring your enum the first way allows you to use it like so:

TokenType my_type;

If you use the second style, you'll be forced to declare your variable like this:

enum TokenType my_type;

As mentioned by others, this doesn't make a difference in C++. My guess is that either the person who wrote this is a C programmer at heart, or you're compiling C code as C++. Either way, it won't affect the behaviour of your code.

Ryan Fox
Your question is correct only for C, but not C++. In C++ enums and structs can be used directly as if there was a typedef.
David Rodríguez - dribeas
Well, yes but this does answer the real question that was asked which was really about "what does this even mean?"
BobbyShaftoe
Good point, dribeas.
Ryan Fox
+5  A: 

It's a C heritage, in C, if you do :

enum TokenType
{
    blah1   = 0x00000000,
    blah2   = 0X01000000,
    blah3   = 0X02000000
};

you'll have to use it doing something like :

enum TokenType foo;

But if you do this :

typedef enum e_TokenType
{
    blah1   = 0x00000000,
    blah2   = 0X01000000,
    blah3   = 0X02000000
} TokenType;

You'll be able to declare :

TokenType foo;

But in C++, you can only do the first one and use it as if it were in a C typedef.

mat
What you say is true in C. It is not true in C++.
Jonathan Leffler
Isn't what I said in my last sentence ?
mat
+2  A: 

You do not need to do it. In C (not C++) you were required to use enum Enumname to refer to a data element of the enumerated type. To simplify it you were allowed to typedef it to a single name data type.

typedef enum MyEnum { 
  //...
} MyEnum;

allowed functions taking a parameter of the enum to be defined as

void f( MyEnum x )

instead of the longer

void f( enum MyEnum x )

Note that the name of the typename does not need to be equal to the name of the enum. The same happens with structs.

In C++, on the other hand, it is not required, as enums, classes and structs can be accessed directly as types by their names.

// C++
enum MyEnum {
   // ...
};
void f( MyEnum x ); // Correct C++, Error in C
David Rodríguez - dribeas