makeArray

Create an array of T with length elements using alloc. The array is either default-initialized, filled with copies of init, or initialized with values fetched from range.

  1. T[] makeArray(Allocator alloc, size_t length)
  2. T[] makeArray(Allocator alloc, size_t length, T init)
  3. Unqual!(ElementEncodingType!R)[] makeArray(Allocator alloc, R range)
    version(HasDRuntime)
    Unqual!(ElementEncodingType!R)[]
    makeArray
    (
    Allocator
    R
    )
    (
    auto ref Allocator alloc
    ,)
    if (
    isInputRange!R &&
    !isInfinite!R
    )
  4. T[] makeArray(Allocator alloc, R range)
  5. eponymoustemplate canSafelyDeallocPostRewind(T)

Parameters

alloc Allocator

the allocator used for getting memory

range R

range used for initializing the array elements

Return Value

Type: Unqual!(ElementEncodingType!R)[]

The newly-created array, or null if either length was 0 or allocation failed.

Throws

The first two overloads throw only if alloc's primitives do. The overloads that involve copy initialization deallocate memory and propagate the exception if the copy operation throws.

Examples

static void test(T)()
{
    T[] a = theAllocator.makeArray!T(2);
    assert(cast(int[])a == [0, 0]);
    a = theAllocator.makeArray!T(3, 42);
    assert(cast(int[])a == [42, 42, 42]);
    import std.range : only;
    a = theAllocator.makeArray!T(only(42, 43, 44));
    assert(cast(int[])a == [42, 43, 44]);
}
test!int();
test!(shared int)();
test!(const int)();
test!(immutable int)();

Meta