Jump to content

  • Log In with Google      Sign In   
  • Create Account

typedef enum declaration confusion


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 ^DJ_Link^   Members   -  Reputation: 148

Like
0Likes
Like

Posted 30 August 2012 - 03:00 AM

Hi everyone. So I never noticed this until today.

I usually declare something like:

typedef enum
{
Action_IDLE   = 0,
Action_BORED  = 1,
Action_WALK   = 2
}Object_Action;

And I can do this

Object_Action action = Action_IDLE;


Looking at "Class View" in Visual Studio I noticed that instead of the list having the name "Object_Action" it's named as "__unnamed_enum_012b_1" .

But if I declare it like

typedef enum Object_Action <- notice the diference
{
Action_IDLE   = 0,
Action_BORED  = 1,
Action_WALK   = 2
}Object_Action;

The name is displayed properly and they both compile. So what's the difference then? From a logical point of view.

Thanks

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8605

Like
5Likes
Like

Posted 30 August 2012 - 03:35 AM

There are two things:
  • typedef A B; makes B a type alias for A.
  • enum {...}; is an anonymous enum definition. It is anonymous since it has no type name associated with it.
Thus, replacing the anonymous enum definition from 2 into A in 1, B becomes a type alias for an anonymous enum. Note that a typedef does not make a new type, but simply makes an alias to the source type, so two typedefs with different names but that represent an alias to the same type are not different types and can be use interchangeably.

To expand on the enum syntax a bit also:
  • enum C {...} D; declares a new type name C, and instantiates a variable named D of said type. Both C and D are optional.
  • If C is omitted, then the type is anonymous, otherwise the enum is named.
  • If D is omitted, then no variable is instantiated.
If the type is unnamed, you cannot instantiate any variables of the type other than as with D using the syntax in point 1 directly when the enum is defined. You can still access all the enum values, but only as an integer value and not as the enum's own type.

The same rules applies to struct, class, union what whatever construct I may have forgotten, possibly with some minor difference.

edit: And I almost forgot the conclusion why VS is displaying an unnamed type: because it is an unnamed type. Sometimes it will/can use the typedef name (not talking about enums, but typedefs in general, for example std::string being a typedef for a more complex template class in which case the std::string is displayed instead of the actual long type name). But strictly speaking, the type is indeed an unnamed type.

Edited by Brother Bob, 30 August 2012 - 03:38 AM.


#3 rip-off   Moderators   -  Reputation: 8727

Like
4Likes
Like

Posted 30 August 2012 - 03:37 AM

The typedef enum { ... } Object_Action; is a C idiom because enums, like structs, have a separate namespace by default:
// No typedefs
enum Object_Action { /* ... */ };

// Need to use the "enum" keyword to compile this in C
enum Object_Action action = Action_IDLE; 
A common variant on this idiom is to given the enum a name and a typedef: typedef enum Foo { ... } Foo, essentially the "workaround" you are using.

C++ removed this namespacing, I imagine because because approximately nobody wants to use it. In C++ you can just have:
enum Object_Action { /* ... */ };

Object_Action action = Action_IDLE;


#4 ^DJ_Link^   Members   -  Reputation: 148

Like
1Likes
Like

Posted 30 August 2012 - 05:02 AM

Thank you rip-off and Brother Bob for the explanation. It really helped




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS