D aliases vs. C typedefs

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 10 13:05:19 PDT 2014


On 06/10/2014 12:40 PM, Tom Browder via Digitalmars-d-learn wrote:

 > I haven't found a detailed description of simple aliases.  TPDL shows
 > aliases of this form:
 >
 >    alias TYPE NAME;

That's the old syntax. As you note below, the newer syntax uses the = 
sign. My alias chapter uses the new syntax as well:

   http://ddili.org/ders/d.en/alias.html

 > // note the two forms: "alias TYPE NAME;" and "alias NAME = TYPE;"
 > alias int val0;            // C: typedef int val0; // okay

Yes, the old syntax.

 > alias val1 = int;         // C: typedef int val1;  // okay

Yes, new syntax.

 > alias val2 = int[2];     // C: typedef int val2[2]; // a 2-element
 >                                     //                array of ints 
(int[2])?

val2 means a 2-element static array of ints:

     static assert(is (val2 == int[2]));
     static assert(is (typeof(val2.init[0]) == int));

 > alias int[2] val3;        // C: typedef int val3[2]; // a 2-element
 > array of ints (int2)?

Same as above. int[2] means a 2-element static array of ints.

 > //alias val4[2] = int[2]; // D error (okay, understandable)
 >
 > // these compile but have no C equivalent that I know of, but what do
 > // the aliases represent?
 >
 > alias int[2] val5[2];     // D: a 2-dimensional array of ints? 
(int[2][2]) ?

Pretty strange. :)

     pragma(msg, val5);

outputs this:

int[2][2]

 > alias int[4] val6[2];     // D:  a 2-dimensional array of ints? 
(int[4][2]) ?
 > alias int    val7[2];      // D:  a 1-dimensional array of ints? 
(int[2]) ?

I don't know whether those are legal. I hope not. :)

Ali



More information about the Digitalmars-d-learn mailing list