Output range with custom string type

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 31 04:44:36 PDT 2017


On Thursday, 31 August 2017 at 07:06:26 UTC, Jacob Carlborg wrote:
> On 2017-08-29 19:35, Moritz Maxeiner wrote:
>
>>      void put(T t)
>>      {
>>          if (!store)
>>          {
>>              // Allocate only once for "small" vectors
>>              store = alloc.makeArray!T(8);
>>              if (!store) onOutOfMemoryError();
>>          }
>>          else if (length == store.length)
>>          {
>>              // Growth factor of 1.5
>>              auto expanded = alloc.expandArray!char(store, 
>> store.length / 2);
>>              if (!expanded) onOutOfMemoryError();
>>          }
>>          assert (length < store.length);
>>          moveEmplace(t, store[length++]);
>>      }
>
> What's the reason to use "moveEmplace" instead of just 
> assigning to the array: "store[length++] = t" ?

The `move` part is to support non-copyable types (i.e. T with 
`@disable this(this)`), such as another owning container 
(assigning would generally try to create a copy).
The `emplace` part is because the destination `store[length]` has 
been default initialized either by makeArray or expandArray and 
it doesn't need to be destroyed (a pure move would destroy 
`store[length]` if T has a destructor).


More information about the Digitalmars-d-learn mailing list