typedef behavior

Ali Çehreli acehreli at yahoo.com
Sat Feb 10 01:01:39 UTC 2018


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.

 > }
 >
 > 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.

 > }
 >
 > 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