Inconsistent behavior shared struct vs non-shared storage - bug?

apz28 home at home.com
Wed Jun 2 12:25:54 UTC 2021


import std.stdio;

enum sharedit = false;

__gshared int counter = 0;

class C
{
}

     struct S
     {
         C c;
         this(C c)
         {
             counter++; writeln("this(): ", counter);
             this.c = c is null ? new C() : c;
         }

         ~this()
         {
             counter--; writeln("~this(): ", counter);
         }

         int foo()
         {
             writeln("foo(): ", counter);
             return 1;
         }
     }

static if (sharedit) shared S s;
else __gshared S s;

     shared static this()
     {
         writeln("shared static this(1): ", counter);
         static if (sharedit) s = cast(shared)S(null);
         else s = S(null);
         writeln("shared static this(2): ", counter);
     }

     void main()
     {
         writeln("main(1): ", counter);
         static if (sharedit) auto i = (cast()s).foo();
         else auto i = s.foo();
         writeln("main(2): ", counter);
     }


/* sharedit = true
shared static this(1): 0
this(): 1
~this(): 0
shared static this(2): 0
main(1): 0
foo(): 0
main(2): 0
*/

/* sharedit = false
shared static this(1): 0
this(): 1
shared static this(2): 1
main(1): 1
foo(): 1
main(2): 1
*/



More information about the Digitalmars-d mailing list