Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2002-06-26 14:14:08


On Tue, 25 Jun 2002 09:49:23 +0200, Daniel Frey
<daniel.frey_at_[hidden]> wrote:

>Dietmar Kuehl wrote:
>>
>> On Monday 24 June 2002 13:48, Daniel Frey wrote:
>> > I noticed that boost doesn't have any helper for sizeof_array.
>>
>> One of the very first submissions to Boost was my "array.h" header which
>> hasn't made it from the beta directory into the approved library part yet: It
>> also contains obviously useful function 'begin()' and 'end()' for the obvious
>> propose.
>

Oh, I didn't follow the bureaucratic path of the library. Now I see
why nobody replied when I asked whether a static const member giving
the count of the array was going to be added to the array_traits
specializations for built-in arrays :-)

At that time I asked that, because it would have been useful to
simplify the following simple generalization of Dietmar Kuehl's sizer
(precisely to avoid the class template "counter"):

#include <boost/array_traits.hpp>
#include <cstddef>

template <std::size_t D, typename T>
struct array_sizer {
        enum { v = array_sizer<D-1, typename
               boost::array_traits<T>::value_type >::v };
};

template <typename Arr>
struct array_sizer<0, Arr>
{
    template <typename T> struct counter;
    template <typename T, std::size_t N> struct counter <T[N]> {
     
        enum { count = N };
    };

        public: enum { v = counter <Arr>::count };
};

// non constant
//
template <std::size_t Dim, typename Array>
std::size_t size (Array&) {
 return array_sizer<Dim, Array>::v;
}

// returns a reference to an array of array_sizer<Dim,T[N]>::v chars
//
template <std::size_t Dim, typename T, std::size_t N>
char (&xsizer( T(&)[N])) [array_sizer<Dim, T[N]>::v];

The xsizer template is used this way:

    char arr [10][20];
    std::size_t n = sizeof (xsizer<1>(arr)); // gives 20

But I suspect there's not much interest in this.

Genny.


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