The X Macro using D
Patrick Schluter via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jul 21 04:19:47 PDT 2017
On Thursday, 20 July 2017 at 21:17:45 UTC, Walter Bright wrote:
> Some time ago, I wrote about the X Macro in C:
>
> https://digitalmars.com/articles/b51.html
>
> I used it from time to time in C code. It's one of the things I
> actually like about the C preprocessor. But in translating the
> aged C code to D it was time to make X work in D. Here's the C
> code, followed by the D translation.
>
In C there's no point in the X macro anymore since C99.
Designated initializer allow to do it properly[1] now.
enum COLORS { red, blue, green, max };
char *cstring[max] = {[red]="red", [blue]="blue",
[green]="green" }; /* C99 */
It works also with array indexes[2].
int a[3] = { [2]=1, [0]=3, [1]=2 }; /* C99 designated
initializer */
int a[3] = { [2]=1, [0]=3, 2 }; /* C99 designated
initializer */
C++ hasn't yet integrated.
[1]: https://dlang.org/ctod.html#arrayenum
[2]: https://dlang.org/ctod.html#arrayinit2
More information about the Digitalmars-d
mailing list