26 Containers library [containers]

26.3 Sequence containers [sequences]

26.3.7 Class template array [array]

26.3.7.1 Class template array overview [array.overview]

The header <array> defines a class template for storing fixed-size sequences of objects. An array is a contiguous container. An instance of array<T, N> stores N elements of type T, so that size() == N is an invariant.

An array is an aggregate that can be list-initialized with up to N elements whose types are convertible to T.

An array satisfies all of the requirements of a container and of a reversible container ([container.requirements]), except that a default constructed array object is not empty and that swap does not have constant complexity. An array satisfies some of the requirements of a sequence container. Descriptions are provided here only for operations on array that are not described in one of these tables and for operations where there is additional semantic information.

namespace std {
  template <class T, size_t N>
  struct array {
    //  types:
    using value_type             = T;
    using pointer                = T*;
    using const_pointer          = const T*;
    using reference              = T&;
    using const_reference        = const T&;
    using size_type              = size_t;
    using difference_type        = ptrdiff_t;
    using iterator               = implementation-defined; // see [container.requirements]
    using const_iterator         = implementation-defined; // see [container.requirements]
    using reverse_iterator       = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    // no explicit construct/copy/destroy for aggregate type

    void fill(const T& u);
    void swap(array&) noexcept(is_nothrow_swappable_v<T>);

    // iterators:
    constexpr iterator               begin() noexcept;
    constexpr const_iterator         begin() const noexcept;
    constexpr iterator               end() noexcept;
    constexpr const_iterator         end() const noexcept;

    constexpr reverse_iterator       rbegin() noexcept;
    constexpr const_reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator       rend() noexcept;
    constexpr const_reverse_iterator rend() const noexcept;

    constexpr const_iterator         cbegin() const noexcept;
    constexpr const_iterator         cend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept;
    constexpr const_reverse_iterator crend() const noexcept;

    // capacity:
    constexpr bool      empty() const noexcept;
    constexpr size_type size() const noexcept;
    constexpr size_type max_size() const noexcept;

    // element access:
    constexpr reference       operator[](size_type n);
    constexpr const_reference operator[](size_type n) const;
    constexpr reference       at(size_type n);
    constexpr const_reference at(size_type n) const;
    constexpr reference       front();
    constexpr const_reference front() const;
    constexpr reference       back();
    constexpr const_reference back() const;

    constexpr T *       data() noexcept;
    constexpr const T * data() const noexcept;
  };

  template<class T, class... U>
    array(T, U...) -> array<T, 1 + sizeof...(U)>;
}

26.3.7.2 array constructors, copy, and assignment [array.cons]

The conditions for an aggregate shall be met. Class array relies on the implicitly-declared special member functions ([class.ctor], [class.dtor], and [class.copy]) to conform to the container requirements table in [container.requirements]. In addition to the requirements specified in the container requirements table, the implicit move constructor and move assignment operator for array require that T be MoveConstructible or MoveAssignable, respectively.

template<class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>;

Requires: (is_­same_­v<T, U> && ...) is true. Otherwise the program is ill-formed.

26.3.7.3 array specialized algorithms [array.special]

template <class T, size_t N> void swap(array<T, N>& x, array<T, N>& y) noexcept(noexcept(x.swap(y)));

Remarks: This function shall not participate in overload resolution unless N == 0 or is_­swappable_­v<T> is true.

Effects: As if by x.swap(y).

Complexity: Linear in N.

26.3.7.4 array​::​size [array.size]

template <class T, size_t N> constexpr size_type array<T, N>::size() const noexcept;

Returns: N.

26.3.7.5 array​::​data [array.data]

constexpr T* data() noexcept; constexpr const T* data() const noexcept;

Returns: A pointer such that data() == addressof(front()), and [data(), data() + size()) is a valid range.

26.3.7.6 array​::​fill [array.fill]

void fill(const T& u);

Effects: As if by fill_­n(begin(), N, u).

26.3.7.7 array​::​swap [array.swap]

void swap(array& y) noexcept(is_nothrow_swappable_v<T>);

Effects: Equivalent to swap_­ranges(begin(), end(), y.begin()).

[Note: Unlike the swap function for other containers, array​::​swap takes linear time, may exit via an exception, and does not cause iterators to become associated with the other container. end note]

26.3.7.8 Zero sized arrays [array.zero]

array shall provide support for the special case N == 0.

In the case that N == 0, begin() == end() == unique value. The return value of data() is unspecified.

The effect of calling front() or back() for a zero-sized array is undefined.

Member function swap() shall have a non-throwing exception specification.

26.3.7.9 Tuple interface to class template array [array.tuple]

template <class T, size_t N> struct tuple_size<array<T, N>> : integral_constant<size_t, N> { };

tuple_element<I, array<T, N>>::type

Requires: I < N. The program is ill-formed if I is out of bounds.

Value: The type T.

template <size_t I, class T, size_t N> constexpr T& get(array<T, N>& a) noexcept; template <size_t I, class T, size_t N> constexpr T&& get(array<T, N>&& a) noexcept; template <size_t I, class T, size_t N> constexpr const T& get(const array<T, N>& a) noexcept; template <size_t I, class T, size_t N> constexpr const T&& get(const array<T, N>&& a) noexcept;

Requires: I < N. The program is ill-formed if I is out of bounds.

Returns: A reference to the Ith element of a, where indexing is zero-based.