Last update April 5, 2010

Doc Comments /

Difference (last change) (no other diffs, normal page display)

Changed: 43c43

There is a limited constructor-type interface: See "static initialization of structs"

Structs & Unions

More Information

align example

The ability to specify alignment is mentioned but the syntax for doing so is not given. As far as I can tell you prefix the struct or union with the alignment requirements using align(n). So to get a struct where all members are 1-byte aligned you would do:

   align(1) struct Foo {
      char aChar;
      int  anInt;

sizeof property for unions

.sizeof is undocumented for unions

Passing structures into and out of functions

Details on how structures are passed into and out of functions would be useful. Are they defaultly passed by value? Are they returned by value? There is a comment that suggests you should not use structs as properties because they are returned by reference. - BradBeveridge?

As far as I know, they're always passed by value, no matter how big they are. -- BillBaxter

Why no constructors?

For POD-types constructors can make code much more readable, take for example a struct to represent a 3D vector:

struct vec3{
    double x,y,z;

    // would be nice to be able to do this...
    this(double X, double Y, double Z){x=X; y=Y; z=Z;}

As I understand it, the main requirement is that structs and unions must be trivially copiable, so it would make sense that copy-constructors and copy-assignment-operators are disallowed, but constructors in general?

(corollary question: why is the assignment operator allowed for structs without this caveat?)

There is a limited constructor-type interface: See "static initialization of structs"


Put your comments about the official/non-official page here.


See the corresponding page in the D Specification: DigitalMars:d/struct

FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: April 5, 2010 5:32 (diff))