Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-12-07 11:18:06


Jonathan Turkanis wrote:

>I skipped the thread about discovering array sizes until tonight, when I
>discovered I need the size of an array type as a compile-time constant --
>without partial specialization. Reading through the discussion, I didn't
>find a proprosed implementation.

>Is this simple implementation correct?

> template<typename T>
> struct array_size {
> static const T* t;
> BOOST_STATIC_CONSTANT(size_t, value = sizeof(T) / sizeof(*t[0]));
> };

>For good measure, one could add
>BOOST_STATIC_ASSERT(boost::is_array<T>::value).

It works for me:

   typedef int iarray[ 10 ];
   std::cout << "size( iarray ) = " << array_size< iarray >::value << '\n';

output:
   size( iarray ) = 10

Thorsten Ottosen wrote:

>the problem is often that you want the size of a variable and not a
>type, eg,

>sizer( my_array_variable );

>which makes the code look a lot different.

In this case, you need a macro:

#define BOOST_ARRAYSIZE( a ) ( sizeof( a ) / sizeof( a[ 0 ] ))

I can't see any other way of doing this on a variable.

Regards,
Reece

_________________________________________________________________
Get Hotmail on your mobile phone http://www.msn.co.uk/msnmobile


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