View source code
Display the source code in object.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function object.capacity

(Property) Gets the current capacity of a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.

size_t capacity(T) (
  T[] arr
) pure nothrow @property @trusted;

If an append must reallocate a slice with no possibility of extension, then 0 is returned. This happens when the slice references a static array, or if another slice references elements past the end of the current slice.


The capacity of a slice may be impacted by operations on other slices.


//Static array slice: no capacity
int[4] sarray = [1, 2, 3, 4];
int[]  slice  = sarray[];
writeln(sarray.capacity); // 0
//Appending to slice will reallocate to a new array
slice ~= 5;
assert(slice.capacity >= 5);

//Dynamic array slices
int[] a = [1, 2, 3, 4];
int[] b = a[1 .. $];
int[] c = a[1 .. $ - 1];
debug(SENTINEL) {} else // non-zero capacity very much depends on the array and GC implementation
    assert(a.capacity != 0);
    assert(a.capacity == b.capacity + 1); //both a and b share the same tail
assert(c.capacity == 0);              //an append to c must relocate c.


Walter Bright, Sean Kelly


Boost License 1.0.