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

std.array.staticArray - multiple declarations

Function staticArray

Constructs a static array from a dynamic array whose length is known at compile-time. The element type can be inferred or specified explicitly:

T[n] staticArray(T, ulong n) (
  auto ref T[n] a
);

U[n] staticArray(U, T, ulong n) (
  auto ref T[n] a
)
if (!is(T == U) && is(T : U));

* [1, 2].staticArray returns int[2] * [1, 2].staticArray!float returns float[2]

Note

staticArray returns by value, so expressions involving large arrays may be inefficient.

Parameters

NameDescription
a The input array.

Returns

A static array constructed from a.

Example

static array from array literal

auto a = [0, 1].staticArray;
static assert(is(typeof(a) == int[2]));
writeln(a); // [0, 1]

Example

static array from array with implicit casting of elements

auto b = [0, 1].staticArray!long;
static assert(is(typeof(b) == long[2]));
writeln(b); // [0, 1]

Function staticArray

Constructs a static array from a range. When a.length is not known at compile time, the number of elements must be given as a template argument (e.g. myrange.staticArray!2). Size and type can be combined, if the source range elements are implicitly convertible to the requested element type (eg: 2.iota.staticArray!(long[2])).

auto staticArray(ulong n, T) (
  scope T a
)
if (isInputRange!T);

auto staticArray(ulong n, T) (
  scope T a,
  out size_t rangeLength
)
if (isInputRange!T);

auto staticArray(Un, U, ulong n, T) (
  scope T a
)
if (isInputRange!T && is(ElementType!T : U));

auto staticArray(Un, U, ulong n, T) (
  scope T a,
  out size_t rangeLength
)
if (isInputRange!T && is(ElementType!T : U));

auto staticArray(alias a)()
if (isInputRange!(typeof(a)));

auto staticArray(U, alias a)()
if (isInputRange!(typeof(a)));

When the range a is known at compile time, it can be given as a template argument to avoid having to specify the number of elements (e.g.: staticArray!(2.iota) or staticArray!(double, 2.iota)).

Parameters

NameDescription
a The input range. If there are less elements than the specified length of the static array, the rest of it is default-initialized. If there are more than specified, the first elements up to the specified length are used.
rangeLength Output for the number of elements used from a. Optional.

Example

static array from range + size

import std.range : iota;

auto input = 3.iota;
auto a = input.staticArray!2;
static assert(is(typeof(a) == int[2]));
writeln(a); // [0, 1]
auto b = input.staticArray!(long[4]);
static assert(is(typeof(b) == long[4]));
writeln(b); // [0, 1, 2, 0]

Example

static array from CT range

import std.range : iota;

enum a = staticArray!(2.iota);
static assert(is(typeof(a) == int[2]));
writeln(a); // [0, 1]

enum b = staticArray!(long, 2.iota);
static assert(is(typeof(b) == long[2]));
writeln(b); // [0, 1]

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.