Live data from Hacker News

Arrays of Arrays (2009)

ericlippert.com

11–20 of 30 posts

Re: Arrays of Arrays (2009)

#11
post #9
post #3

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(…

With C++20, use of comma expressions as second argument of a subscript operator got deprecated.

... with the intention of allowing multidimensional indexing in the next standard.

Re: Arrays of Arrays (2009)

#12
post #3

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(…

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) 
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)

#13
post #9
post #3

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(…

With C++20, use of comma expressions as second argument of a subscript operator got deprecated.

And it is awful. See my comment about yalsat's use of asserts() above.

Re: Arrays of Arrays (2009)

#14
Which is why the sane way to do it is to have the array-specifier come before the element type:

  [,][]int crazy;
It reads "2-dimensional array of array of int" in L-to-R order.

Similarly, the type "pointer-to-int" should be "*p" or equivalent.

Unfortunately, history/tradition as well parsing problems make the "sane" solution difficult. One solution is the Pascal approach putting the type after the variable being declared, with a colon between:

  crazy : [,][]int;

Re: Arrays of Arrays (2009)

#15
post #3

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(…

> 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)

#16
post #5
post #3

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(…

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

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)

#17

Which is why the sane way to do it is to have the array-specifier come before the element type: [,][]int crazy; It reads "2-dimensional array of array of int" in L-to-R order. Similarly, the type "pointer-to-int" should be "*p" or equivalent. Unfortunately, history/tradition as well parsing problems make the "sane" solution difficult. One solution is the Pascal approach putting the type after the variable being decla…

Strongly agreed! During my PhD I had to design array type syntax for a programming language, and we eventually came up with something similar to that: https://futhark-lang.org/blog/2016-12-09-two-syntax-design-p...

Re: Arrays of Arrays (2009)

#18
post #5
post #3

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(…

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

They'd probably be lowered to exactly the same code, so the compiler argument isn't very good.

Re: Arrays of Arrays (2009)

#19
post #7

This reasoning seems to me similar to how declaring a pointer in e.g. C works: many beginners write int* a, b; And think that this constructs two pointers-to-integer. In fact, it constructs one pointer and one plain integer. A better (equivalent) way to write it is int *a, b; You're not declaring variables of type int* You're saying that b is an int and *a Is an int, where the asterisk means "the dereferenced a"!

Beginners and also C++ programmers, expert and beginner alike. Even though it’s just as misleading in C++. This:

    int a = 0;
    int& x = a, y = a;
declares x to be a reference, but y an independent variable. Similarly, a template parametrised by a variadic pack of function pointers is declared thus:

    template 
It’s a good thing I don’t work in C++ too often, because my forehead would have probably sustained noticeable injury from all the facepalming each time I see ‘int& x’ instead of ‘int &x’ and ‘typename... T’ instead of ‘typename ...T’.

Re: Arrays of Arrays (2009)

#20
post #12
post #3

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(…

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) These are definitions for typed dynamic arrays using C macros. Very useful…

> 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.

Post reply on HN