typedef behavior

Alex sascha.orlov at gmail.com
Sat Feb 10 01:14:24 UTC 2018


On Saturday, 10 February 2018 at 01:01:39 UTC, Ali Çehreli wrote:
> On 02/09/2018 03:45 PM, Alex wrote:
> > A question about Typedef usage:
> > Say, I have the following circumstances
> >
> > /// --- code --- ///
> >
> > import std.typecons;
> >
> > void main()
> > {
> >      MyEA ea;
> >      MyEB eb;
> >      ea.tarr.length = 5;
> >      static assert(!is(MyEA == MyEB));
> >      static assert(!is(MyEA == E));
> >      static assert(!is(MyEB == E));
> >      assert(ea.tarr.length == eb.tarr.length); // line 11
> >      assert(ea.tarr.length != eb.tarr.length); // line 12
>
> You must have meant
>
>      assert(ea.tarr.ptr != eb.tarr.ptr); // line 12
>
> Indeed, .ptr are unexpectedly the same.
>

Yes... this is a more precise comparison :)

> > }
> >
> > struct T
> > {
> >      size_t i;
> > }
> >
> > struct E
> > {
> >      size_t i;
> >      static T[] tarr;
>
> To save time to others, note that 'tarr' is a static member 
> that ends up being shared by two Typedef instantiations.

Yup. They are shared by two Typedef instantiations with different 
cookies.

So...
The question is two-fold:
Would it help to alter the init value of the Typedef?
If yes, how to alter it?
If no, is this a bug?

>
> > }
> >
> > alias MyEA = Typedef!(E, E.init, "A"); // line 26
> > alias MyEB = Typedef!(E, E.init, "B"); // line 27
> >
> > /// --- code ends --- ///
> >
> > Line 12 yields an assertion error, while line 11 does not.
> > This tells me, that despite the fact the types MyEA and MyEB
> are
> > different they still share the static array, which would
> contradict the
> > definition of static.
> >
> > I suppose, the tricky thing is to tweak the init property of
> the
> > typedefs in lines 26/27 to avoid this clash. How to manage
> this?
>
> Ali


More information about the Digitalmars-d-learn mailing list