Boost logo

Boost :

From: Nicolai Josuttis (nicolai.josuttis_at_[hidden])
Date: 2000-01-09 07:58:26


attached is a new version of array.hpp.
See array.html for a description of the features.

However, as a short overview the changes according
to recent discussions:
 - name is array
 - using initializer lists is supported
 - therefore elements with uninitialized values are possible
 - new functions: front(), back(), at(), ==, !=, <, >, <=, >=, swap(),
 - reverse iterator support (rbegin(), rend())
 - a better documentation (array.html)

Note that I didn't follow the following suggestions:
 - providing abilities such as the reverse stuff as base class
   (because this would disallow initializer support)
 - providing another version as some kind of "vector with constant size"
 - providing initializations and assignments from other sequences
   (as the size is fixed, they don't make much sense)

Note the open issues described in array.html.

I know that this version dos NOT fit everbodys wishes.
Take it as NEXT step to start supplementary discussions about this class.
Of course, the MOST important question is whether we want
to have initializer list support.
Note BTW, that
 array<int,6> a = { 1, 2, 3, 4 };
seems to be wrong.
It has to be:
 array<int,6> a = { { 1, 2, 3, 4 } };
DO WE WANT THIS or would Gregs proposals be better:
> int data[] = { 0,1,2,3,4 };
> array<int,5> x(data);
> or maybe
> int data[] = { 0,1,2,3,4 };
> array<int,data> x;

Comments please!

Nicolai M. Josuttis 
Solutions in Time        	mailto:solutions_at_[hidden]

array.hpp, an STL Array Wrapper  
Class array, an STL Container (as Wrapper) for Arrays of Constant Size

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 vector<>. However, 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 - 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.

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 class is not an STL container because:

It doesn't fulfill the requirements of a sequence, except that

As a special feature, the container is an aggregate (see Section 8.5.1, [dcl.init.aggr], of the C++ Standard). Therefore

The code is provided "as is" without expressed or implied warranty.

Open issues:

I'd appreciate any constructive feedback.

array.hpp, the implementation of array<>:

  • as HTML file
  • as plain file
  • Simple Example for using carray<>:

  • as HTML file
  • as plain file
  • Another Example for using carray<>:

  • as HTML file
  • as plain file
  • An third Example for using carray<>:

  • as HTML file
  • as plain file
  • All files

  • as ZIP file (12KB)
  • as TGZ file (7KB)

    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

  • Home Page

    Boost list run by bdawes at, gregod at, cpdaniel at, john at