View source code Display the source code in object.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

Function object.capacity

(Property) Get 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.

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.

Prototype

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

Note

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

Example

//Static array slice: no capacity
int[4] sarray = [1, 2, 3, 4];
int[]  slice  = sarray[];
assert(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.

Authors

Walter Bright, Sean Kelly

License

Boost License 1.0.

Comments