Array

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 Nicolai Josuttis presents slightly modified in his book The C++ Standard Library, called carray. This is the essence of these approaches spiced with many feedback from boost.

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

Usage

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> 
    bool operator==(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N> 
    bool operator!=(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N> 
    bool operator<(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N> 
    bool operator>(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N> 
    bool operator<=(const array<T, N>&, const array<T, N>&);
  template<typename T, std::size_t N> 
    bool operator>=(const array<T, N>&, const array<T, N>&);
}

Here is the details:


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/destruct
  template<typename U> array& operator=(const array<U, N>&);

  // iterator support
  iterator begin();
  const_iterator begin() const;
  iterator end();
  const_iterator end() const;

  // reverse iterator support
  reverse_iterator rbegin();
  const_reverse_iterator rbegin() const;
  reverse_iterator rend();
  const_reverse_iterator rend() const;

  // capacity
  size_type size();
  bool empty();
  size_type max_size();

  // element access
  reference operator[](size_type);
  const_reference operator[](size_type) const;
  reference at(size_type);
  const_reference at(size_type) const;
  reference front();
  const_reference front() const;
  reference back();
  const_reference back() const;
  const T* data() const;
  T* c_array();

  // modifiers
  void swap(array<T, N>&);
  void assign(const T&);

  T elems[N];
};

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

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

Design

Here was an important design tradeoff regarding the constructors: We could implement array as an "aggregate". 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.