Boost logo

Boost :

From: Alisdair Meredith (alisdair.meredith_at_[hidden])
Date: 2003-08-11 12:25:15


Robert Ramey wrote:

> const int array[] = {1, 2, 3, 4};
> boost::container_facade<int> cfa(array, sizeof(array)/sizeof(int));

I guess my problem is I still don't understand what is wrong with

const boost::array< int, 4 > = { 1, 2, 3, 4 };

in this case.

If you are referring to having to dictate the size of the array in
advance, rather than deduce it, that is a known deficiency and I would
welcome any solutions for this!

[I have proposed array to the ISO committee and this is one of the
issues that I suspect may hold back its adoption]

As for making the copy above:
Would a constructor template perform the deduction correctly?

template< typename T, unsigned int N >
container_facade( const T[N] source )
{
  std::copy( source, source + N, m_data );
}

We are still stuck with the case that N does not match the fixed size of
the array template, but we are closer for my case (a) where the array
size is fixed at run-time rather than compile time.

By using a function template as a factory, we could even deduce the
correct array size, at the expense of another copy.

template< typename T, unsigned int N >
boost::array<T, N> make_array( const T[N] source )
{
  boost::array<T, N> result;
  std::copy( source, source + N, result.begin() );
  return result;
}

I can't help feel you are trying to use array for something it is not.
As I see it, boost::array is a replacement for 'traditional' arrays, not
a wrapper for them, in the same way that std::vector is a replacement
for dynamic arrays.

-- 
AlisdairM

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