Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-06-25 04:29:02


Daniel Frey 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.
>
> Time to look at it again? Maybe we can merge the best of it into a new
> header/library. What I noticed about your array_traits.h are three
> [snipp]
> Before we start implementing anything, we should agree on the objectives
> of such a library. I'd prefer not to mix STL-stuff with the "old"
> arrays. Other opinions?

Impatient as I am, I couldn't stop merging it, leaving out the
STL-stuff. :)

Here's the result so far:

--- array_traits.hpp --- (new version)

#if !defined( BOOST_ARRAY_TRAITS_HPP )
#define BOOST_ARRAY_TRAITS_HPP

#include <cstddef>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

namespace boost
{
   template< typename T > class array_traits
   {
   private:
      BOOST_STATIC_ASSERT( ::boost::is_array< T >::value );

   public:
      typedef typename remove_bounds< T >::type value_type;
      typedef typename add_pointer< value_type >::type iterator;
      
      BOOST_STATIC_CONSTANT( std::size_t, size = sizeof( T ) / sizeof(
value_type ) );
   };

   namespace array
   {
      template< typename T > inline std::size_t size( T& )
      {
         return ::boost::array_traits< T >::size;
      }
   
      template< typename T > inline array_traits< T >::iterator begin(
T& array )
      {
         return array;
      }
   
      template< typename T > inline array_traits< T >::iterator end( T&
array )
      {
         return array + size( array );
      }
   }
}

#endif // !defined( BOOST_ARRAY_TRAITS_HPP )

--- t.cc --- (my small test program)

#include <iostream>
#include <algorithm>
using namespace std;

#include "array_traits.hpp"

int main()
{
   const char c[] = "Hello, World!";
   cout << boost::array::size( c ) << endl;

   double x[] = { 123.45, 124.32, 125.21 };
   typedef int X[ 42 ];

   enum {
      V1 = boost::array_traits< X >::size,
      V2 = boost::array_traits< __typeof( x ) >::size
   };

   cout << V1 << endl
        << V2 << endl;

   ostream_iterator< double > oit( cout, " " );
   copy( boost::array::begin( x ), boost::array::end( x ), oit );
   
   cout << endl;
}

--- the end ---

Note that the test program uses GCC's __typeof.

Regards, Daniel

--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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