Does D provide automatic dereferencing for accessing members through pointers?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 28 22:48:12 PDT 2014


On 08/28/2014 09:37 PM, Andrew Godfrey wrote:

 > On Friday, 29 August 2014 at 02:10:46 UTC, H. S. Teoh via
 > Digitalmars-d-learn wrote:
 >> In D you just use '.' throughout and it Just > Works(tm).
 >
 > Unless the property you're accessing is also a pointer property, like
 > sizeof. Then you have to be careful.

The same applies to class variables. .sizeof and .alignof operate on the 
class variable. To get the object's size and alignment, one needs to 
reach for classInstanceSize and classInstanceAlignment.

Unfortunately, for historical reasons they have dissimilar syntax:

import std.string;

class C
{
     ubyte[42] a;
}

void main()
{
     auto c = new C();

     pragma(msg, format("size     : %s vs %s", c.sizeof,
                        __traits(classInstanceSize, C)));

     import std.traits;
     pragma(msg, format("alignment: %s vs %s", c.alignof,
                        classInstanceAlignment!C));
}

Ali



More information about the Digitalmars-d-learn mailing list