From: tmb@arolla.idiap.ch (Thomas M. Breuel)
Newsgroups: comp.lang.c++
Subject: Re: Proposal: Subscripting with multiple arguments
Date: 16 Sep 92 02:33:37 GMT
	<MATT.92Sep14161955@physics.berkeley.edu>
Reply-To: tmb@idiap.ch
Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
	Perceptive)
In-reply-to: matt@physics.berkeley.edu's message of 14 Sep 92 23:19:55 GMT

In article <MATT.92Sep14161955@physics.berkeley.edu> matt@physics.berkeley.edu (Matt Austern) writes:

   In article <1992Sep14.182942.1986@athena.mit.edu> mrhagger@athena.mit.edu (Michael R Haggerty) writes:

   > I have been using C++ for numerical work, and one of the changes which would
   > be most useful would be to allow the subscription operator to take multiple
   > parameters.  This would make it much simpler to define classes for multi-
   > dimensional matrices without the nuisance of having to define a helper class.
   > The concept should not be such a shock to anyone, as the subscript operator
   > is already allowed to take non-integer arguments so is already significantly
   > extended from the C subscript operator.

   I've also occasionally wished that I could define, say, 
	   operator[](int,int,int), 
   and for the same reason: multi-dimensional arrays.  I haven't wished
   for it very often, though, because that isn't really the syntax that I
   want for a multi-dimensional array anyway; I would much rather access
   an array element using the syntax A[i][j] than using the syntax
   A[i,j].  After all, the A[i][j] syntax is what you would use if you
   were using built-in data types instead of classes, and it's nice to
   keep some things the same when defining a class.  (For that matter,
   there isn't really anything wrong with the syntax A(i,j), either.)

I have been using operator()(int,int,...) exclusively for
multidimensional arrays, and it works nicely. Many other languages
use this notation, and some even consider arrays simply a special
kind of function anyway.

   What I find to be a bigger problem, actually, is designing an array
   class that works for arrays with an arbitrary number of dimensions.
   There's no problem with defining the classes Array1, Array2,
   Array3,..., and that's exactly what I've done, but it's annoying and
   inelegant.

I don't think it is inelegant. After all, a 1D array and a 2D array
are very different kinds of objects. Why should they have the same
type? You would lose some of the advantages of type checking that way.

Also, I have found that I rarely need arrays of dimension higher than
3 anyway, since most arrays either represent regularly spaced values
in 1D, 2D, or 3D Euclidean space, or a sequence of objects indexed by
intervals over the integers. Most uses of higher dimensional arrays
really tend to be collections of lower-dimensional arrays, which are
more naturally represented as such (e.g., Array< Array< Array2<float> > >).

What you could do, however, is to share a lot of code among the
different array classes. For example, you might have your
multidimensional array inherit from your 1D array. Still, I have found
it easier to write separate 1D, 2D, and 3D array template classes.

					Thomas.
Newsgroups: comp.lang.c++