AggregateDeclaration: Tag { DeclDefs } Tag Identifier { DeclDefs } Tag Identifier ; Tag: struct unionThey work like they do in C, with the following exceptions:
struct ABC x;are not allowed, replace with:
ABC x;
struct X { int a; int b; int c; int d = 7;} static X x = { a:1, b:2}; // c is set to 0, d to 7 static X z = { c:4, b:5, a:2 , d:5}; // z.a = 2, z.b = 5, z.c = 4, z.d = 5C-style initialization, based on the order of the members in the struct declaration, is also supported:
static X q = { 1, 2 }; // q.a = 1, q.b = 2, q.c = 0, q.d = 7
union U { int a; double b; } static U u = { b : 5.0 }; // u.b = 5.0Other members of the union that overlay the initializer, but occupy more storage, have the extra storage initialized to zero.
.sizeof Size in bytes of struct .size Same as .sizeof .alignof Size boundary struct needs to be aligned on
EnumDeclaration: enum identifier { EnumMembers } enum { EnumMembers } enum identifier ; EnumMembers: EnumMember EnumMember , EnumMember , EnumMembers EnumMember: Identifier Identifier = ExpressionEnums replace the usual C use of #define macros to define constants. Enums can be either anonymous, in which case they simply define integral constants, or they can be named, in which case they introduce a new type.
enum { A, B, C } // anonymous enumDefines the constants A=0, B=1, C=2 in a manner equivalent to:
const int A = 0; const int B = 1; const int C = 2;Whereas:
enum X { A, B, C } // named enumDefine a new type X which has values X.A=0, X.B=1, X.C=2
Named enum members can be implicitly cast to integral types, but integral types cannot be implicitly cast to an enum type.
Enums must have at least one member.
If an Expression is supplied for an enum member, the value of the member is set to the result of the Expression. The Expression must be resolvable at compile time. Subsequent enum members with no Expression are set to the value of the previous member plus one:
enum { A, B = 5+7, C, D = 8, E }Sets A=0, B=12, C=13, D=8, and E=9.
.min Smallest value of enum .max Largest value of enum .sizeof Size of storage for an enumerated value For example: X.min is X.A X.max is X.C X.sizeof is same as int.sizeof
enum X { A=3, B, C } X x; // x is initialized to 3