Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualBrother Bob

Posted 30 August 2012 - 03:38 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.

#1Brother Bob

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.

PARTNERS