Listing 6: Resize member function

// Set the size of each array dimension
bool resize(const unsigned int (&Dimensions)[N], 
            const T & Init=T(), 
            bool PreserveElems=false)
{
  Array<T,N> Temp; 

  // Calculate all the information you need to use the array
  Temp.m_nArrayElements=1;
  for (int i=0; i<N; i++)
  {
     if (Dimensions[i]==0) 
        return false;    // Check that no dimension was zero 
     Temp.m_nArrayElements*=Dimensions[i]; 
     Temp.m_NDimensions[i]=Dimensions[i];
     Temp.m_SubArrayLen[i]=1;              
     for (int k=N-1; k>i; k--)
        Temp.m_SubArrayLen[i]*=Dimensions[k];
  }  

  // Allocate new elements, let exception propagate 
  Temp.m_pArrayElements=new T[Temp.m_nArrayElements];

  // Some compilers might not
  // throw exception if allocation fails
  if (!Temp.m_pArrayElements)
     return false;

  // Copy the elements from the previous array if requested
  if (PreserveElems && !empty())
      Temp.copy(*this, Init);
  // Otherwise initialize them to the specified value
  else 
      Temp.initialize(Init);

  // Now swap this object with the temporary
  swap(Temp);

  return true; 
}