· Lexical · Modules · Declarations · Types · Properties · Attributes · Pragmas · Expressions · Statements · Arrays · Structs & Unions · Classes · Interfaces · Enums · Functions · Operator Overloading · Templates · Mixins · Contracts · Conditional Compilation · Handling errors · Garbage Collection · Memory Management · Floating Point · Inline Assembler · Documentation Comments · Interfacing To C · Portability Guide · Embedding D in HTML · Named Character Entities · Application Binary Interface |
Structs & UnionsAggregateDeclaration: Tag { DeclDefs } Tag Identifier StructBody Tag Identifier ; Tag: struct union StructBody: { } { StructBodyDeclarations } StructBodyDeclarations: StructBodyDeclaration StructBodyDeclaration StructBodyDeclarations StructBodyDeclaration: Declaration Invariant UnitTest StructAllocator StructDeallocator StructAllocator: ClassAllocator StructDeallocator: ClassDeallocatorThey work like they do in C, with the following exceptions:
Static Initialization of StructsStatic struct members are by default initialized to whatever the default initializer for the member is, and if none supplied, to the default initializer for the member's type. If a static initializer is supplied, the members are initialized by the member name, colon, expression syntax. The members may be initialized in any order. Members not specified in the initializer list are default initialized.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 Static Initialization of UnionsUnions are initialized explicitly.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. Struct Properties.sizeof Size in bytes of struct .alignof Size boundary struct needs to be aligned on Struct Field Properties.offsetof Offset in bytes of field from beginning of struct |