public member function
<array>

std::array::array

// aggregate type (no custom constructors)
Construct array
The array classes are aggregate types, and thus have no custom constructors.

As aggregate classes they can be constructed by means of the special member functions defined implicitly for classes (default, copy, move), or by using initializer lists:

  • default-initialization: Each of the elements is itself default-initialized.
    For elements of a class type this means that their default constructor is called.
    For elements of fundamental types, they are left uninitialized (unless the array object has static storage, in which case they are zero-initialized).
  • copy/move initialization: The elements of an array object of the exact same type (and size) are copied/moved to the constructed array.
  • initializer list: The values in the list are taken as initializers for the elements in the array.
    If the list has fewer initializers than elements in the array, the remaining elements are value-initialized (i.e., for class types, the default constructor is called; for fundamental types, they are zero-initialized).
    The initializer list shall not have more initializers than elements.

Essentially, they are initialized in the same way as built-in arrays, except for the following differences:
  • The size of array objects cannot be determined by the number of initializers in the initializer list. It is always determined by the second template argument.
  • array objects can be copied/moved (also on assignments).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// constructing arrays
#include <iostream>
#include <array>

// default initialization (non-local = static storage):
std::array<int,3> global;               // zero-initialized: {0,0,0}

int main ()
{
  // default initialization (local = automatic storage):
  std::array<int,3> first;              // uninitialized:    {?,?,?}

  // initializer-list initializations:
  std::array<int,3> second = {10,20};   // initialized as:   {10,20,0}
  std::array<int,3> third = {1,2,3};    // initialized as:   {1,2,3}

  // copy initialization:
  std::array<int,3> fourth = third;     // copy:             {1,2,3}

  std::cout << "The contents of fourth are:";
  for (auto x:fourth) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Output:
The contents of fourth are: 1 2 3