View source code
Display the source code in std/experimental/allocator/package.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 std.experimental.allocator.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.

T[] makeArray(T, Allocator) (
  auto ref Allocator alloc,
  size_t length
);

T[] makeArray(T, Allocator) (
  auto ref Allocator alloc,
  size_t length,
  T init
);

Unqual!(ElementEncodingType!R)[] makeArray(Allocator, R) (
  auto ref Allocator alloc,
  R range
)
if (isInputRange!R && !isInfinite!R);

T[] makeArray(T, Allocator, R) (
  auto ref Allocator alloc,
  R range
)
if (isInputRange!R && !isInfinite!R);

Parameters

NameDescription
T element type of the array being created
alloc the allocator used for getting memory
length length of the newly created array
init element used for filling the array
range range used for initializing the array elements

Returns

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.

Example

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

Authors

Andrei Alexandrescu

License

Boost License 1.0.