Why emsi containers have @disabled this(this) ?

Dukc ajieskola at gmail.com
Wed May 20 09:24:49 UTC 2020


On Tuesday, 19 May 2020 at 20:51:01 UTC, Luis wrote:
> I saw that they have postblit operator... But i don't 
> understand exactly why. In special, when they implement 
> InputRange over the containers, but having disabled postblit, 
> make nearly useless (at least as I see on this old post 
> https://forum.dlang.org/thread/n1sutu$1ugm$1@digitalmars.com?page=1 )

You have to understand the memory management difference between 
EMSI-containers and regular D slices. A normal slice won't worry 
about freeing the memory used. It just assumes that either the 
garbage collector will take care of it (the usual case), or that 
the code will tell manually when the memory really needs to be 
freed (possibly by some other reference to the same memory).

EMSI-containers, however, are designed to free their memory after 
use themselves. They do it by the Resource Acquisition Is 
Initialization principle: when the container gets deleted, the 
underlying data gets freed right away.

This leads to that there must be only one container using the 
same memory. It would not do to make a copy of it, unless it's a 
deep copy (Deep copy means that also the underlying memory is 
copied. It tends to take long.). This is the reason that 
postblits are disabled in EMSI-containers. It wants to protect 
you from accidents. If you want to store the same container in 
many places, store references to it instead. And when iterating 
over it, you get the underlying range first, like Mike said. The 
underlying range can likely be copied freely.


More information about the Digitalmars-d-learn mailing list