Boost logo

Boost :

From: Larry Evans (jcampbell3_at_[hidden])
Date: 2001-03-27 11:26:16


Serge Barral wrote:

>
> To improve safety, a constructor that takes a "shape_data"
> object which is parametrized with the number of dimensions could be
> provided. I mean something like:
>
> multi_array( const shape_data< NumDims >& )
>
> An array could then be instantiated in this way:
>
> multi_array< double, 3 > a( shape[3][2][10] );
>
> to make sure at compile-time that the shape is consistant with the rank of
> the array (I also find the notation convenient).
>
> Here is a quick and dirty implementation :
>
> template< std::size_t rank > class shape_data {
> public:
> shape_data( std::size_t first_dims[rank-1] , std::size_t last_dim )
> {
> for ( std::size_t i=0; i!=(rank-1); ++i )
> _dim[i] = first_dims[i];
> _dim[rank-1] = last_dim;
> }
>
> shape_data<rank+1> operator[] ( std::size_t n )
> {
> return shape_data<rank+1>( _dim, n );
> }
> private:
> std::size_t _dim[rank];
> };
>
> struct shape_builder {
> shape_data<1> operator[] ( std::size_t n ) {
> return shape_data<1>( 0, n );
> }
> };
>
> shape_builder shape;
>

If the compiler instantiates the whole class instead of just the member functions, then
you'll have to terminate the template recursion with a max rank specialization of
shape_data. This was a problem with g++ a little before 1996.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk