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.

Parameters

T

element type of the array being created

alloc
Type: Allocator

the allocator used for getting memory

range
Type: R

range used for initializing the array elements

Return Value

Type: T[]

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

1 import std.algorithm.comparison : equal;
2 static void test(T)()
3 {
4     T[] a = theAllocator.makeArray!T(2);
5     assert(a.equal([0, 0]));
6     a = theAllocator.makeArray!T(3, 42);
7     assert(a.equal([42, 42, 42]));
8     import std.range : only;
9     a = theAllocator.makeArray!T(only(42, 43, 44));
10     assert(a.equal([42, 43, 44]));
11 }
12 test!int();
13 test!(shared int)();
14 test!(const int)();
15 test!(immutable int)();

Meta