Mixing std.container.array and Appender

user1234 user1234 at 12.de
Mon Feb 9 11:13:40 UTC 2026


On Monday, 9 February 2026 at 10:53:32 UTC, user1234 wrote:
> On Wednesday, 4 February 2026 at 08:45:06 UTC, axricard wrote:
>> Hello, is there some structure in standard library that acts 
>> like Appender but works also on std.container.array Arrays ?
>> I'd like to have the capacity of Arrays doubling every time 
>> new memory is allocated.
>> Appender requires the input to be a dynamic array, which is 
>> not the case for std.container.array Arrays.
>
> To the cost of small run-time overhead one solution is to 
> forward appending in a custom function, e.g
>
> ```d
> void append(T)(ref T arr, int elem)
> {
> 	arr ~= elem;
>
> 	static if (is(T == std.container.array.Array!int))
> 		arr.reserve(nextPow2(arr.capacity-1));
>
> 	writefln("capacity after append: %u", arr.capacity);
> }
> ```

my bad, I meant

```d
arr.reserve(nextPow2(arr.length+1));
```


More information about the Digitalmars-d-learn mailing list