C macros vs D can't do the right thing

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 3 07:03:25 PDT 2017


On Saturday, 3 June 2017 at 13:17:46 UTC, Russel Winder wrote:
> A stripped down problem to avoid fluff. The C macro:
>
>     #define FLOB(t) (sizeof(t))
>
> Can be used in another macro:
>
>     #define THINGY(a, b) (_THING(a, FLOB(b)))
>
> We can use this as in:
>
>     THINGY(10, __u32)
>
> Now the D Way says use functions not macros. except that
>
>     size_t FLOB(T)(auto ref T t) { return t.sizeof; }
>
> which is the result of DStep doesn't allow passing a type name 
> as t.
>
> Is this a problem in D or a problem in DStep?

I think an alias template parameter will work here as aliases 
take anything(types, literals, symbols).


More information about the Digitalmars-d-learn mailing list