D structs weak identity and RAII

Boris-Barboris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 19 03:13:55 PDT 2017


On Monday, 19 June 2017 at 06:34:49 UTC, Ali Çehreli wrote:
> It's unreliable because structs are value types in D, which 
> means that they can be moved around freely. This is why 
> self-referencing structs are illegal in D.

I guess it's more like the spec states, that they can be moved 
vithout notice. Value type semantics themselves do not mean 
spontaneous uncontrolled mobility.

> I can't claim expertise but here is a quick and dirty proof of 
> concept that Vittorio Romeo and I had played with a few weeks 
> ago:
>
> ...
>
> shared byte b; // to prevent compiler optimizations
>
> struct UniquePtr {
>     void * p;
>
>     this(void * p) {
>         this.p = p;
>         import core.atomic;
>         core.atomic.atomicOp!"+="(b, 1);
>     }

Very interesting hack, thank you.

> Note how post-blit is disabled there. In addition to a 
> moveFrom(), that's exactly what Atila Neves had to do in this 
> automem library as well:
>
>   https://github.com/atilaneves/automem

Nice, thanks.




More information about the Digitalmars-d-learn mailing list