Ronald Garcia
On Jan 16, 2007, at 3:51 PM, Matthias Troyer wrote:
On Jan 16, 2007, at 4:59 PM, Ronald Garcia wrote:
Hello Markus,
On Jan 16, 2007, at 9:06 AM, Markus Werle wrote:
IMHO the behaviour of operator= for multi_array is a little bit broken. A lot of preconditions required:
Indeed operator= for multi_array requires the sizes of the multi_arrays to match, hence the assertions. The multi_array type is not meant to automatically resize when assigned to. All resizing is intentionally meant to be explicit. For your application, you will need to resize your array prior to assignment.
This is awkward since it means that any class with a multi_array member cannot use the default operator= but needs to implement a special operator=. We just ran into a subtle bug with this because of the semantics of the valarray operator=, and I would strongly argue for an operator = that makes the semantics of
A x = y;
equivalent to
A x; x = y;
Hi Matthias,
multi_array's operator=() was designed to behave in a manner consistent with the other array objects provided by the library (subarrays, views, multi_array_ref). In particular, assignment between any of these other types requires that the dimensions of the arrays match, and it performs an assignment to the analogous locations in the array. I am loath to alter these semantics.
To Ron's credit, the documentation does not say that multi_array models Assignable. If it did, it would have to obey the semantics listed above for any two multi_arrays that have the same template parameters. I am curious to know the source of Ron's loathing, though. But I agree with Markus' point that modeling Assignable is a good thing. It means multi_array would become Regular since it already has equality and a total ordering (assuming the element type does). This would make multi_array a more useful class, in my opinion.
One possible approach, which I'm not completely happy with, is to provide special behavior for default constructed multi_arrays. In particular, assignment to a default-constructed multi_array would cause it to resize during assignment. This would make such default- constructed multi_arrays special.
I don't think creating more special cases is the way to proceed. The question seems to be: are two multi_arrays with the same template parameters the same type or not? Right now the answer is "sometimes" because of assignment. I think everyone would be better served if the answer was either "yes" or "no." I will vote "yes" if given a chance, because it will simplify the class instead of making it more complex. If the answer is "no", the dimensions might as well be set at compile time. Sincerely, Mark