Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Introduction

The C++ Standard Template Library STL as part of the C++ Standard Library provides a framework for processing algorithms on different kind of containers. However, ordinary arrays don’t provide the interface of STL containers (although, they provide the iterator interface of STL containers).

As replacement for ordinary arrays, the STL provides class std::vector. However, std::vector<> provides the semantics of dynamic arrays. Thus, it manages data to be able to change the number of elements. This results in some overhead in case only arrays with static size are needed.

In his book, Generic Programming and the STL, Matthew H. Austern introduces a useful wrapper class for ordinary arrays with static size, called block. It is safer and has no worse performance than ordinary arrays. In The C++ Programming Language, 3rd edition, Bjarne Stroustrup introduces a similar class, called c_array, which I (Nicolai Josuttis) present slightly modified in my book The C++ Standard Library - A Tutorial and Reference, called carray. This is the essence of these approaches spiced with many feedback from Boost.

After considering different names, we decided to name this class simply array.

Note that this class is suggested to be part of the next Technical Report, which will extend the C++ Standard (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1548.htm).

Update: std::array is (as of C++11) part of the C++ standard. The differences between boost::array and std::array are minimal. If you are using C++11, you should consider using std::array instead of boost::array.

Class array fulfills most but not all of the requirements of "reversible containers" (see Section 23.1, [lib.container.requirements] of the C++ Standard). The reasons array is not an reversible STL container is because:

  • No constructors are provided.

  • Elements may have an undetermined initial value (see the section called "Design Rationale").

  • swap() has no constant complexity.

  • size() is always constant, based on the second template argument of the type.

  • The container provides no allocator support.

It doesn’t fulfill the requirements of a "sequence" (see Section 23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that:

  • front() and back() are provided.

  • operator[] and at() are provided.

Revision History

Changes in 1.88.0

  • Converted documentation to AsciiDoc (Christian Mazakas).

  • Added noexcept and constexpr as appropriate.

  • Marked obsolete functions as deprecated.

  • Removed obsolete compiler workarounds.

  • Changed array<T, 0>::begin(), cbegin(), end(), cend() to return nullptr, enabling constexpr. This matches the behavior of std::array.

  • Removed local hash_value overload; boost::hash supports array-like types natively.

  • array<T, 0> can now be initialized with = {{}}.

  • Added operator<=>.

  • Added to_array.

Reference

Header <boost/array.hpp>

namespace boost {

  template<typename T, std::size_t N> class array;

  template<typename T, std::size_t N>
    void swap(array<T, N>&, array<T, N>&);

  template<typename T, std::size_t N>
    constexpr bool operator==(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N>
    constexpr bool operator!=(const array<T, N>&, const array<T, N>&);

  template<typename T, std::size_t N>
    constexpr bool operator<(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N>
    constexpr bool operator>(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N>
    constexpr bool operator<=(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N>
    constexpr bool operator>=(const array<T, N>&, const array<T, N>&);

  template<typename T, std::size_t N>
    constexpr auto operator<=>(const array<T, N>&, const array<T, N>&);

  template<std::size_t Idx, typename T, std::size_t N>
    constexpr T& get(array<T, N>&) noexcept;
  template<std::size_t Idx, typename T, std::size_t N>
    constexpr const T& get(const array<T, N>&) noexcept;

  template<class T, std::size_t N>
    constexpr array<T, N> to_array( T const (&)[N] );
  template<class T, std::size_t N>
    constexpr array<T, N> to_array( T (&&)[N] );
  template<class T, std::size_t N>
    constexpr array<T, N> to_array( T const (&&)[N] );
}

Class template array

Synopsis

// In header: <boost/array.hpp>

template<typename T, std::size_t N>
class array {
public:

  // types

  typedef T                                     value_type;
  typedef T*                                    iterator;
  typedef const T*                              const_iterator;
  typedef std::reverse_iterator<iterator>       reverse_iterator;
  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  typedef T&                                    reference;
  typedef const T&                              const_reference;
  typedef std::size_t                           size_type;
  typedef std::ptrdiff_t                        difference_type;

  // static constants

  static const size_type static_size = N;

  // construct/copy/destroy

  template<typename U> array& operator=(const array<U, N>&);

  // iterator support

  constexpr iterator begin() noexcept;
  constexpr const_iterator begin() const noexcept;
  constexpr const_iterator cbegin() const noexcept;

  constexpr iterator end() noexcept;
  constexpr const_iterator end() const noexcept;
  constexpr const_iterator cend() const noexcept;

  // reverse iterator support

  reverse_iterator rbegin() noexcept;
  const_reverse_iterator rbegin() const noexcept;
  const_reverse_iterator crbegin() const noexcept;

  reverse_iterator rend() noexcept;
  const_reverse_iterator rend() const noexcept;
  const_reverse_iterator crend() const noexcept;

  // capacity

  static constexpr size_type size() noexcept;
  static constexpr bool empty() noexcept;
  static constexpr size_type max_size() noexcept;

  // element access

  constexpr reference operator[](size_type);
  constexpr const_reference operator[](size_type) const;

  constexpr reference at(size_type);
  constexpr const_reference at(size_type) 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;

  T* c_array() noexcept; // deprecated

  // modifiers

  swap(array<T, N>&);

  constexpr void fill(const T&);
  void assign(const T&); // deprecated

  // public data members
  T elems[N];
};

Construct/Copy/Destroy

template<typename U> array& operator=(const array<U, N>& other);
Effects:

For each i in [0..N), performs elems[i] = other.elems[i];.


Iterator Support

constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
Returns:

data().


constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;
Returns:

data() + size().


Reverse Iterator Support

reverse_iterator rbegin() noexcept;
Returns:

reverse_iterator(end()).


const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
Returns:

const_reverse_iterator(end()).


reverse_iterator rend() noexcept;
Returns:

reverse_iterator(begin()).


const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
Returns:

const_reverse_iterator(begin()).


Capacity

static constexpr size_type size() noexcept;
Returns:

N.


static constexpr bool empty() noexcept;
Returns:

N == 0.


static constexpr size_type max_size() noexcept;
Returns:

N.


Element Access

constexpr reference operator[](size_type i);
constexpr const_reference operator[](size_type i) const;
Requires:

i < N.

Returns:

elems[i].

Throws:

Nothing.


constexpr reference at(size_type i);
constexpr const_reference at(size_type i) const;
Returns:

elems[i].

Throws:

std::out_of_range if i >= N.


constexpr reference front();
constexpr const_reference front() const;
Requires:

N > 0.

Returns:

elems[0].

Throws:

Nothing.


constexpr reference back();
constexpr const_reference back() const;
Requires:

N > 0.

Returns:

elems[N-1].

Throws:

Nothing.


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

elems.


T* c_array() noexcept; // deprecated
Returns:

data().

Remarks:

This function is deprecated. Use data() instead.


Modifiers

void swap(array<T, N>& other);
Effects:

std::swap(elems, other.elems).

Complexity:

linear in N.


void fill(const T& value);
Effects:

For each i in [0..N), performs elems[i] = value;.


void assign(const T& value); // deprecated
Effects:

fill(value).

Remarks:

An obsolete and deprecated spelling of fill. Use fill instead.


Specialized Algorithms

template<typename T, std::size_t N>
  void swap(array<T, N>& x, array<T, N>& y);
Effects:

x.swap(y).


Comparisons

template<typename T, std::size_t N>
  constexpr bool operator==(const array<T, N>& x, const array<T, N>& y);
Returns:

std::equal(x.begin(), x.end(), y.begin()).


template<typename T, std::size_t N>
  constexpr bool operator!=(const array<T, N>& x, const array<T, N>& y);
Returns:

!(x == y).


template<typename T, std::size_t N>
  constexpr bool operator<(const array<T, N>& x, const array<T, N>& y);
Returns:

std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()).


template<typename T, std::size_t N>
  constexpr bool operator>(const array<T, N>& x, const array<T, N>& y);
Returns:

y < x.


template<typename T, std::size_t N>
  constexpr bool operator<=(const array<T, N>& x, const array<T, N>& y);
Returns:

!(y < x).


template<typename T, std::size_t N>
  constexpr bool operator>=(const array<T, N>& x, const array<T, N>& y);
Returns:

!(x < y).


template<typename T, std::size_t N>
  constexpr auto operator<=>(const array<T, N>& x, const array<T, N>& y)
  -> decltype(x[0] <=> y[0]);
Effects:

For each i in [0..N), if (x[i] <=> y[i]) != 0, returns x[i] <=> y[i]. Otherwise, returns std::strong_ordering::equal, converted to the return type.

Remarks:

When N is 0, the return type is std::strong_ordering and the return value is std::strong_ordering::equal.


Specializations

template<std::size_t Idx, typename T, std::size_t N>
  constexpr T& get(array<T, N>& arr) noexcept;
Mandates:

Idx < N.

Returns:

arr[Idx].


template<std::size_t Idx, typename T, std::size_t N>
  constexpr const T& get(const array<T, N>& arr) noexcept;
Mandates:

Idx < N.

Returns:

arr[Idx].


Creation

template<class T, std::size_t N>
  constexpr array<T, N> to_array( T const (&a)[N] );
Returns:

an array<T, N> r such that for each i in [0..N), r[i] is copied from a[i].

template<class T, std::size_t N>
  constexpr array<T, N> to_array( T (&&a)[N] );
Returns:

an array<T, N> r such that for each i in [0..N), r[i] is moved from std::move(a[i]).

template<class T, std::size_t N>
  constexpr array<T, N> to_array( T const (&&a)[N] );
Returns:

an array<T, N> r such that for each i in [0..N), r[i] is copied from a[i].


Design Rationale

There was an important design tradeoff regarding the constructors: We could implement array as an "aggregate" (see Section 8.5.1, [dcl.init.aggr], of the C++ Standard). This would mean:

  • An array can be initialized with a brace-enclosing, comma-separated list of initializers for the elements of the container, written in increasing subscript order:

    boost::array<int,4> a = { { 1, 2, 3 } };

    Note that if there are fewer elements in the initializer list, then each remaining element gets default-initialized (thus, it has a defined value).

However, this approach has its drawbacks: passing no initializer list means that the elements have an indetermined initial value, because the rule says that aggregates may have:

  • No user-declared constructors.

  • No private or protected non-static data members.

  • No base classes.

  • No virtual functions.

Nevertheless, the current implementation uses this approach.

Note that for standard conforming compilers it is possible to use fewer braces (according to 8.5.1 (11) of the Standard). That is, you can initialize an array as follows:

boost::array<int,4> a = { 1, 2, 3 };

For more information…​

To find more details about using ordinary arrays in C++ and the framework of the STL, see e.g.

The C++ Standard Library - A Tutorial and Reference
by Nicolai M. Josuttis
Addison Wesley Longman, 1999
ISBN 0-201-37926-0

Copyright © 2001-2004 Nicolai M. Josuttis

Copyright © 2012 Marshall Clow

Distributed under the Boost Software License, Version 1.0.