Hello Johann, On Jul 11, 2008, at 6:17 AM, Dr Johann A. Briffa wrote:
I realize this topic has been discussed before (I've seen at least a thread in 2006 and another in 2007), but I believe there is more to add. At issue is the decision that multi_array objects cannot be assigned [with the operator=() that is] unless the target is already the same size as the source. Further, there is no documented way to easily resize an existing array to the same size and range as some other array.
Indeed there is no built-in way to easily resize a multi_array to be
the same size as another multi_array. While there is no resize member
function that takes a pointer, there is one that takes a model of the
Collection concept (classes like boost::array and std::vector are
concepts). Based on this interface, here is a stop-gap non-invasive
solution that can resize a multi_array based on another model of
MultiArray (multi_array, multi_array_ref, etc.):
//
=
=
=
=
=
=
=
=
=
=
========================================================================
#include
As far as my needs are concerned, a multi_array that cannot be assigned is next to useless. Allow me to elaborate: I am using multi_array as a replacement to my own (old) 2/3D containers, as recently I've had need for higher dimensions. I have classes containing array members, where the size of the arrays is dynamic through the object lifetime. So my class default constructor uses the array default constructor for its member objects, creating empty arrays. Eventually as my classes get used, the array members are updated accordingly, and get to have some non-zero size. Now the problem is that my classes need to be assignable, and the only way that can be achieved is if the array members are assignable; the alternative is to have to write a specific assignment operator for any of my classes that use arrays, resizing each member of the target before copying. Clearly that would involve a lot of extra effort.
I believe that in a previous discussion I had suggested a template argument-based solution to this, where a multi_array type would have resizing assignment (and thereby no longer be a model of the MultiArray concept). I don't recall getting feedback on that proposal. Cheers, ron