How to reserve memory for a slice in a struct

Maxim Fomin maxim at maxim-fomin.ru
Tue May 7 06:37:43 PDT 2013


On Tuesday, 7 May 2013 at 10:29:44 UTC, Namal wrote:
> Hello,
>
> I am new to D. According to that power of 2 rule I want to
> reserve 2 sized chunks to my array, But how do I do that in a
> struct?

You can set some kind of statically allocated array size:

struct S
{
	int[] a;
	this(size_t size)
	{
		a.length = size;
	}
}

enum SE : S
{
	A = S(2)
}

static assert(SE.init.a.length is 2);

void main()
{
	SE se;
	assert(se.a.length is 2);
}

Unfortunately this does not work with array reserve.


More information about the Digitalmars-d-learn mailing list