need clarification: will typedef, C struct initialization, etc.

Adam Ruppe destructionator at gmail.com
Mon May 31 14:11:20 PDT 2010


Doesn't

struct T { R _payload; }

accomplish the same thing as

typedef R T;

in the majority of useful cases?

I use typedef void* Some_C_Type; a lot, and rather like it for a
handful of other things, but I'm pretty sure the struct accomplishes
the same thing in those cases. As a template in the stdlib
(std.typecons surely), it could be done easily enough in user code

The immediate differences are casting doesn't work the same* and the
compiler error messages aren't as good**. But these are minor, and
might be caused solely by my poor implementation.

* I used struct Typedef(R) { R _base; } alias Typedef!int Handle;

Casting:  Error: e2ir: cannot cast a of type Typedef!(int) to type
int. Though, Handle a; a._base; would do the trick, just not be
consistent.

** Using the above:
void foo(T) { }

foo(10); // cannot implicitly convert expression (10) of type int to
Typedef!(int)

Might be nice if it showed the alias name. This is something that
might be generally useful if changed, so it could improve other things
unlike the specialized feature.

On 5/31/10, bearophile <bearophileHUGS at lycos.com> wrote:
> Andrei Alexandrescu:
>> typedef is gone.
>
> *mewls* I have shown here some examples of typedef usage, and I'll keep
> posting few more in future. I'd like to pull it back from the grave and keep
> it :-)
>
> Bye,
> bearophile
>


More information about the Digitalmars-d mailing list