
I've been looking at the implementation of Boost.multi_array, and in particular the mechanics of generating the array extents objects. The problem to be solved is to allow the extent of a multi-dimensional array to be specified with this syntax extents[2][3][4] since this forms a nice nmemonic. The solution is a templated type template <unsigned ndims> class extents_type {.....}; which contains a subscripting operator which returns its own object with an extra subscript tacked-on extents_type<ndims+1> operator[]( unsigned n ) {....} OK. That's all fairly straight-forward. The interesting bit is the specialisation required for the initial dimensionless object. The Boost implementation goes through several intermediate objects which eventually resolve to a boost::array<unsigned, ndims> in general, or a boost::array<unsigned, 1> for the dimensionless case. My questions about this are: * why boost::array<unsigned, 1> rather than boost::array<unsigned, 0>? Obviously an array of zero elements is an odd beast, but it seems to work, with begin() equal to end() as you'd expect. * is it necessary to employ so many different objects to resolve the special case? Is this an aspect of the Boost style guidlelines, or simply some C++ good practise that I've missed? The existing implementation of this is in boost/multi_array/range_list.hpp if you're interested. Thanks, Rob.

On Thu, Oct 16, 2008 at 09:29, Robert Jones <robertgbjones@gmail.com> wrote:
* why boost::array<unsigned, 1> rather than boost::array<unsigned, 0>? Obviously an array of zero elements is an odd beast, but it seems to work, with begin() equal to end() as you'd expect.
As I recall, a zero-length array did not used to work, but TR1 allowed it, so the boost version (relatively recently) added the necessary specialization. I'm fairly sure that multi_array was written before this change.
participants (2)
-
Robert Jones
-
Scott McMurray