News for hackers. Stuff that matters.
Re:Tell HN: C Experts Panel – Ask us anything about C by zokier on Tuesday April 14, 2020 @05:24PM (#22871642)
What's std::array then?

> combines the performance and accessibility of a C-style array with the benefits of a standard container, such as knowing its own size

https://en.cppreference.com/w/cpp/container/array

Re:Tell HN: C Experts Panel – Ask us anything about C by kevin_thibedeau on Tuesday April 14, 2020 @06:45PM (#22872391)
They're objects that mostly behave like arrays. You can't index element two of std::array foo as 1[foo] since it isn't an actual C array.
Re:Tell HN: C Experts Panel – Ask us anything about C by kazinator on Tuesday April 14, 2020 @09:13PM (#22873419)
A Pascal array is just ones and zeros that behave like an array. So is a Fortran array.

> You can't index element two of std::array foo as 1[foo] since it isn't an actual C array.

That's just a silly quirk of C syntax that is deliberately not modeled in C++ operator overloading. It's not a real capability; it doesn't make arrays "do" anything new, so it' hard to call it an array behavior. It's a compiler behavior, that's for sure.

It could easily be added to C++, similarly to the way preincrement and postincrement are represented (which allows that obj++ and ++obj to be separate overloads).

   T &array_class::operator [] (int index) {
      // handles array[42]
   }

   T &array_class::operator [] (int index, int) {  // Fictional!!
      // handles 42[array]
   }
The dummy extra int parameter would mean "this overload of operator [] implements the flipped case, when the object is between the [ ] and the index is on the left".

C++ could easily have this; the technical barrier is almost nonexistent. (I wonder what the minimal diff against GNU C++ would be to get it going.)

I suspect that it's explicitly unwanted.

Re:Tell HN: C Experts Panel – Ask us anything about C by saagarjha on Tuesday April 14, 2020 @07:52PM (#22872871)
Ok, but who actually uses that?
Re:Tell HN: C Experts Panel – Ask us anything about C by kevin_thibedeau on Tuesday April 14, 2020 @09:23PM (#22873482)
The point is to demonstrate that std::array isn't an array.
Re:Tell HN: C Experts Panel – Ask us anything about C by kazinator on Tuesday April 14, 2020 @09:40PM (#22873585)
What makes you so sure that if C got better arrays, those would be arrays, supporting a[i] i[a] commutativity and all?

That is predicated on equivalence to *(a + i) where a is a dumb pointer whose displacement commutes.

Re:Tell HN: C Experts Panel – Ask us anything about C by pjmlp on Wednesday April 15, 2020 @02:45AM (#22875233)
That is a quirk of C's arrays, no other language besides Assembly allows for that.

And even in Assembly, it depends on the CPU flavor which kind of memory accesses are available.

Re:Tell HN: C Experts Panel – Ask us anything about C by kazinator on Wednesday April 15, 2020 @09:51AM (#22877672)
It depends entirely on the whims of the assembly language design. Assembly lanuages for the Motorola 68000 could allow operand syntax like like [A0 + offset], which could commute with [offset + A0], but the predominant syntax for that CPU family has it as offset(A0) which cannot be written A0(offset).

None of that changes what instruction is generated, just like C's quirk is one of pure syntax that doesn't affect the run-time.

Re:Tell HN: C Experts Panel – Ask us anything about C by saagarjha on Tuesday April 14, 2020 @09:26PM (#22873497)
Ok, fair. But for almost all practical purposes, std::array is an appropriate array replacement.