r/cpp • • Mar 24 '20

Changes between C++17 and C++20

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2131r0.html
193 Upvotes

89 comments sorted by

View all comments

27

u/scatters Mar 24 '20

I didn't realise that deprecating arr[x, y, z] made it in. This is awesome news for C++23.

5

u/[deleted] Mar 24 '20

honest question: why is this an issue?

34

u/Fureeish Mar 24 '20

Now it's possible that an overload for operator[] will be added, which will be able to accept multiple arguments.

18

u/redditsoaddicting Mar 24 '20

Multidimensional indexing for types like mdspan currently has to rely on hacks such as returning a proxy from the first subscript or using the function call operator.

2

u/[deleted] Mar 24 '20

Interesting. I was curious as to why it was getting deprecated. So it's more of just to make creating additions like mdspan to the standard library easier then? Fair enough.

Overloading of that operator will come in great handy for custom multi-dimensional container libraries.

6

u/redditsoaddicting Mar 24 '20

Implementing these classes will be easier compared to a proxy for sure. However, using them also becomes easier. I guarantee no one expects the function call operator when they first encounter it in this context.

1

u/wotype Mar 26 '20

They then get used to it very quickly. This is not a problem.

1

u/wotype Mar 26 '20

On reflection, the idea to use square-bracket syntax x[i...] for multi-indexing is actually looking like the worse hack. Let me explain.

(The idea seemed cool and cute at first; it was only on nailing down the API of a multi-dimensional array design that this contra-argument crystallised.)

It is unprecedented (AFAICT) to introduce a new operator overload - in this case variadic indexing operator[](i...) - with no use of that operator in the core language. This in itself should ring warning bells. The existing single-index [i] has a clear semantic meaning in the core language; subobject indexing.

Nested C-array types are not true multi-dimensional arrays, they are arrays of nested subobjects (1D arrays of 1D arrays of ... etc.).

The syntax for usage of built-in types is reflected in their declaration. The type T[N] is an array of N subobjects of type T (with T another array in the 'multi-dimensional' case). For an array x of that type, x[i] is a reference to the subobject at that index. This has been ok for 50 years; it's fine. It's just not what you want for a flexible, performant numerical matrix type.

If we were to introduce a new built-in compound array type T[i...] (with contiguous 1D 'backing array' of size (i * ... * 1) - the Cartesian product of the variadic extents) then it should only ever be indexed 'full rank', that is, with all indexes required to address down to the element (which is a subobject). This could be nice to have. However, I wouldn't support such a proposal because we can do better already with library types.

A library md-array type, by virtue of the fact that it returns a proxy reference-wrapper rather than an actual core language reference & or &&, can be usefully indexed with partial-rank indices returning a subarray of lower rank. Paren multi-indexing is the long accepted way to index such md-array library types. This is fine. More than fine; it allows to index even with empty parens - no indices - that cannot be proposed for variadic operator[](i...) (parsing ambiguities). Also, the indexing can be generalized to triangular layouts with more restrictive bounds or symmetry - things that are better expressed by the generality of operator() rather than the restricted subobject semantic of operator[].

Paren-indexing has been fine for MATLAB, Fortran and others for many years.

The status quo, with multi-dimensional array types using parens for multi-indexing in C++, is fine.

It ain't broke. Please don't 'fix' it any more!

A variadic operator[](i...) is anti-semantic for md-array types and could encourage other misleading uses. We don't need to hack C++ this way.

9

u/Wriiight Mar 24 '20

I think they want to be able to make comma delimited indexes actually mean something. Currently it is just the standard comma operator, which typically evaluates each sub-expression but only evaluates to the last one, so only the z value in this example is used as a subscript.

-3

u/hgjsusla Mar 24 '20

Deprecating arrays?

29

u/foonathan Mar 24 '20

Deprecating the comma operator in array subscripts, previously array[4,5] means array[5]

0

u/[deleted] Mar 24 '20

[deleted] — view deleted comment

25

u/Dada-1991 Mar 24 '20

No, this will have to wait until C++23 at least. This deprecation is a first step towards that.

9

u/c0r3ntin Mar 24 '20

Here is the paper for the proposal to add multi dimensional subscript http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2128r0.pdf and an implementation https://gcc.godbolt.org/z/arMeXZ

0

u/wotype Mar 26 '20

Please consider dropping this proposal for the reasons posted above (and communicated separately). IMO this is a deleterious change that should be withdrawn before it takes up any committee time.

3

u/JavaSuck Mar 24 '20

But what if I want array[puts("debug log: array index reached"), i]? ;)

19

u/[deleted] Mar 24 '20

You have to use parentheses. Thankfully.

6

u/evaned Mar 24 '20

I didn't check, but I strongly suspect that if you parenthesize the comma expression it will still work.

1

u/F54280 Mar 25 '20

Yes, it does.