News for hackers. Stuff that matters.
Re:Arrays of Arrays (2009) by Animats on Saturday December 19, 2020 @11:40PM (#25483914)
Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections. In C++, you can overload "operator[]". But it can't take more than one argument. Why?

Because of the C "comma operator". In C, the comma operator evaluates both operands and discards the first result. It's an ancient hack used mostly with preprocessor defines.

In C and C++,

    foo(a,b)
is a call with two arguments. But

    foo[a,b]
invokes the comma operator and presents one value to "[]" You have to dig through the C++ specification to find this.

So I asked the question, could anyone find an example of the comma operator used in C or C++ inside square brackets in any production code. I couldn't. I got someone at IBM Almaden to search IBM's code archive and they couldn't. But there was the concern that somewhere, someone had used that misfeature for something.

So, no 2D array syntax for you.

Re:Arrays of Arrays (2009) by geocar on Sunday December 20, 2020 @02:23AM (#25484587)
> Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections.

You might've asked operator[] to take multiple arguments separated by semicolon, so:

    foo[a;b]
which is at least unambiguous. Maybe it would have been less offensive.

> So I asked the question, could anyone find an example of the comma operator used in C or C++ inside square brackets in any production code.

If you're still referring to operator overloading, there's things like Boost.Assign and Boost.Phoenix, but if you're referring to use as a sequence operator, i.e. where a[b,c] could potentially be b,a[c] except for the different sequence point, it doesn't surprise me you had a hard time finding an example: I've only ever seen people like Arthur do that (people writing APL in C)

Re:Arrays of Arrays (2009) by tbrgmnn on Sunday December 20, 2020 @03:29AM (#25484812)
With C++20, use of comma expressions as second argument of a subscript operator got deprecated.
Re:Arrays of Arrays (2009) by gpderetta on Sunday December 20, 2020 @05:17AM (#25485202)
... with the intention of allowing multidimensional indexing in the next standard.
Re:Arrays of Arrays (2009) by Animats on Sunday December 20, 2020 @01:01PM (#25488159)
Finally!

Multidimensional array discussions tended to dissolve into bikeshedding. Some people wanted the ability to store an array as either transpose, that is, by row or by column. Then that had to be extended to N dimensions. That added a lot of complexity to support a rare use case. Then the proposal got so complex it was shelved. All this is in old USENET comp.lang.c++, if that hasn't been lost yet.

Re:Arrays of Arrays (2009) by thesz on Sunday December 20, 2020 @06:09AM (#25485440)
And it is awful. See my comment about yalsat's use of asserts() above.
Re:Arrays of Arrays (2009) by thomasahle on Sunday December 20, 2020 @07:18AM (#25485761)
> In C, the comma operator evaluates both operands and discards the first result.

Sounds like how the semicolon operator works in modern languages, like Rust.

Re:Arrays of Arrays (2009) by anonymoushn on Sunday December 20, 2020 @10:20AM (#25486797)
It does not, because this code does not compile:

  fn main() {
      println!("ok"; "Hello, world!");
  }
Re:Arrays of Arrays (2009) by thesz on Sunday December 20, 2020 @06:08AM (#25485432)
Take a look at yalsat: http://fmv.jku.at/yalsat/

Unpack the "v" version, look at yals.c. You will see there the following:

  #define PUSH(S,E) \
  do { \
    if (FULL(S)) ENLARGE (S); \
    *(S).top++ = (E); \
  } while (0)
  
  #define POP(S) \
    (assert (!EMPTY (S)), *--(S).top)
  
  #define TOP(S) \
    (assert (!EMPTY (S)), (S).top[-1])
  
  #define PEEK(S,P) \
    (assert ((P) < COUNT(S)), (S).start[(P)])
  
  #define POKE(S,P,E) \
    do { assert ((P) < COUNT(S)); (S).start[(P)] = (E); } while (0)
These are definitions for typed dynamic arrays using C macros. Very useful.

As you can see, there's a couple of assert()'s before actual computation of the address to fetch.

I hope I proved you wrong.

Re:Arrays of Arrays (2009) by dmurray on Sunday December 20, 2020 @08:34AM (#25486102)
> I hope I proved you wrong.

I don't see where you did - Animats said he couldn't find examples of the comma operator inside square brackets, not that he couldn't see it being used at all.

Re:Arrays of Arrays (2009) by thesz on Sunday December 27, 2020 @05:10PM (#25554710)
I think he has failed precisely because he wanted not to succeed.
Re:Arrays of Arrays (2009) by sesuximo on Sunday December 20, 2020 @12:26AM (#25484078)
Imo foo[x][y] is better for many reasons:

- compiler can break it apart so foo[x] is evaluated once for many ys just like any other function

- you can make types that are unaware of their rank eg vector doesn’t know if it’s a 1D vector of int or a 2D vector of vectors

- “seamlessly” handles jagged arrays

Re:Arrays of Arrays (2009) by jiggawatts on Sunday December 20, 2020 @12:56AM (#25484203)
Which reminds me of an insightful observation that the array indexing operator [] and the function call operator () are basically doing the same thing: evaluating a function and returning a value.

So what you've just described is basically Currying, or partial function evaluation. It's a bit like defining "al(i)" as array lookup for an index 'i' instead of "[i]". Then the following are equivalent:

    something[i][j]
    something.al(i).al(j)
What people also want are:

    something[i,j]
    something.al(i,j)
In languages like Haskell, every function can always be evaluated argument-by-argument, with no special effort by the programmer. The compiler takes care of generating the code for the various intermediate call forms. In languages like C++, it would be a significant hassle to create all of the various partially-evaluated wrapper types.
Re:Arrays of Arrays (2009) by gpderetta on Sunday December 20, 2020 @02:30PM (#25488858)
In C++ you can create a simple generic reusable currying wrapper. Boost as a few. The reality is that the vast majority of C++ programmers don't see the need.
Re:Arrays of Arrays (2009) by Animats on Sunday December 20, 2020 @06:25PM (#25490590)
Trying to do this this through Currying turns a simple 2D array access into a quite complicated operation. One that a compiler has to be really smart to turn into a gheap 2D array indexing operation again. This really matters in number-crunching code, where it seems that almost everything is about multiplying arrays.
Re:Arrays of Arrays (2009) by gpderetta on Tuesday December 22, 2020 @09:39AM (#25506630)
Of course it wouldn't make much sense to do it for indexing. I was discussing currying as a generally useful tool.
Re:Arrays of Arrays (2009) by Athas on Sunday December 20, 2020 @07:43AM (#25485868)
It's not the same when you support slicing. foo[x:y][z] is not the same as foo[x:y,z]. I'm not sure anyone is proposing slicing to be supported with the indexing operator in C++, though, so it might not matter.
Re:Arrays of Arrays (2009) by Bancakes on Sunday December 20, 2020 @04:58AM (#25485127)
It's semantically the same and the compiler should know that. But I do agree [][] makes more sense. The jth index of the ith array.
Re:Arrays of Arrays (2009) by mhh__ on Sunday December 20, 2020 @08:01AM (#25485940)
They'd probably be lowered to exactly the same code, so the compiler argument isn't very good.
Re:Arrays of Arrays (2009) by sesuximo on Sunday December 20, 2020 @09:38AM (#25486465)
If the user has access to both indices, they can make partially hosting harder/impossible
Re:Arrays of Arrays (2009) by dls2016 on Sunday December 20, 2020 @10:29AM (#25486879)
I just learned this one while teaching an intro to C++ class... the student had no idea what they wrote and it took me a minute to realize why their code even compiled.