|
|
|
|
|
by Animats
2011 days ago
|
|
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. |
|
You might've asked operator[] to take multiple arguments separated by semicolon, so:
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)