Skip to content

Commit 3cf7d2b

Browse files
jensmaurertkoeppe
authored andcommitted
P2128R6 Multidimensional subscript operator
1 parent 743fcd7 commit 3cf7d2b

File tree

6 files changed

+41
-43
lines changed

6 files changed

+41
-43
lines changed

source/compatibility.tex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@
4040
\end{codeblock}
4141
\end{example}
4242

43+
\rSec2[diff.cpp20.expr]{\ref{expr}: expressions}
44+
45+
\diffref{expr.sub}
46+
\change
47+
Change the meaning of comma in subscript expressions.
48+
\rationale
49+
Enable repurposing a deprecated syntax to support multidimensional indexing.
50+
\effect
51+
Valid \CppXX{} code that uses a comma expression within a
52+
subscript expression may fail to compile. For example:
53+
\begin{codeblock}
54+
arr[1, 2] // was equivalent to \tcode{arr[(1, 2)]},
55+
// now equivalent to \tcode{arr.operator[](1, 2)} or ill-formed
56+
\end{codeblock}
57+
4358
\rSec2[diff.cpp20.utilities]{\ref{utilities}: general utilities library}
4459

4560
\diffref{format}

source/expressions.tex

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,7 @@
29822982
\begin{bnf}
29832983
\nontermdef{postfix-expression}\br
29842984
primary-expression\br
2985-
postfix-expression \terminal{[} expr-or-braced-init-list \terminal{]}\br
2985+
postfix-expression \terminal{[} \opt{expression-list} \terminal{]}\br
29862986
postfix-expression \terminal{(} \opt{expression-list} \terminal{)}\br
29872987
simple-type-specifier \terminal{(} \opt{expression-list} \terminal{)}\br
29882988
typename-specifier \terminal{(} \opt{expression-list} \terminal{)}\br
@@ -3020,8 +3020,18 @@
30203020
\pnum
30213021
\indextext{operator!subscripting}%
30223022
\indextext{\idxcode{[]}|see{operator, subscripting}}%
3023-
A postfix expression followed by an expression in square brackets is a
3024-
postfix expression. One of the expressions shall be a glvalue of type ``array of
3023+
A \defnadj{subscript}{expression} is a postfix expression
3024+
followed by square brackets containing
3025+
a possibly empty, comma-separated list of \grammarterm{initializer-clause}s
3026+
which constitute the arguments to the subscript operator.
3027+
The \grammarterm{postfix-expression} is sequenced before
3028+
each expression in the \grammarterm{expression-list}.
3029+
3030+
\pnum
3031+
With the built-in subscript operator,
3032+
an \grammarterm{expression-list} shall be present,
3033+
consisting of a single \grammarterm{assignment-expression}.
3034+
One of the expressions shall be a glvalue of type ``array of
30253035
\tcode{T}'' or a prvalue of type ``pointer
30263036
to \tcode{T}'' and the other shall be a prvalue of unscoped enumeration or integral type.
30273037
The result is of type ``\tcode{T}''.
@@ -3036,13 +3046,6 @@
30363046
\tcode{*((E1)+(E2))},
30373047
except that in the case of an array operand, the result is an lvalue
30383048
if that operand is an lvalue and an xvalue otherwise.
3039-
The expression \tcode{E1} is sequenced before the expression \tcode{E2}.
3040-
3041-
\pnum
3042-
A comma expression\iref{expr.comma}
3043-
appearing as the \grammarterm{expr-or-braced-init-list}
3044-
of a subscripting expression is deprecated;
3045-
see \ref{depr.comma.subscript}.
30463049

30473050
\pnum
30483051
\begin{note}
@@ -3052,9 +3055,6 @@
30523055
\tcode{+} and~\ref{dcl.array} for details of array types.
30533056
\end{note}
30543057

3055-
\pnum
3056-
A \grammarterm{braced-init-list} shall not be used with the built-in subscript operator.
3057-
30583058
\rSec3[expr.call]{Function call}
30593059

30603060
\pnum
@@ -7067,7 +7067,9 @@
70677067
\pnum
70687068
\begin{note}
70697069
In contexts where the comma token is given special meaning
7070-
(e.g. function calls\iref{expr.call}, lists of initializers\iref{dcl.init},
7070+
(e.g. function calls\iref{expr.call},
7071+
subscript expressions\iref{expr.sub},
7072+
lists of initializers\iref{dcl.init},
70717073
or \grammarterm{template-argument-list}{s}\iref{temp.names}),
70727074
the comma operator as described in this subclause can appear only in parentheses.
70737075
\begin{example}
@@ -7079,14 +7081,6 @@
70797081
\end{example}
70807082
\end{note}
70817083

7082-
\pnum
7083-
\begin{note}
7084-
A comma expression
7085-
appearing as the \grammarterm{expr-or-braced-init-list}
7086-
of a subscripting expression\iref{expr.sub} is deprecated;
7087-
see \ref{depr.comma.subscript}.
7088-
\end{note}
7089-
70907084
\rSec1[expr.const]{Constant expressions}%
70917085
\indextext{expression!constant}
70927086

source/future.tex

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,6 @@
5858
\end{codeblock}
5959
\end{example}
6060

61-
\rSec1[depr.comma.subscript]{Comma operator in subscript expressions}
62-
63-
\pnum
64-
A comma expression\iref{expr.comma}
65-
appearing as the \grammarterm{expr-or-braced-init-list}
66-
of a subscripting expression\iref{expr.sub} is deprecated.
67-
\begin{note}
68-
A parenthesized comma expression is not deprecated.
69-
\end{note}
70-
\begin{example}
71-
\begin{codeblock}
72-
void f(int *a, int b, int c) {
73-
a[b,c]; // deprecated
74-
a[(b,c)]; // OK
75-
}
76-
\end{codeblock}
77-
\end{example}
78-
7961
\rSec1[depr.array.comp]{Array comparisons}
8062

8163
\pnum

source/overloading.tex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,29 +3297,32 @@
32973297
\pnum
32983298
A \defnadj{subscripting}{operator function}
32993299
is a function named \tcode{\keyword{operator}[]}
3300-
that is a non-static member function with exactly one non-object parameter.
3300+
that is a non-static member function.
33013301
For an expression of the form
33023302
\begin{ncsimplebnf}
3303-
postfix-expression \terminal{[} expr-or-braced-init-list \terminal{]}
3303+
postfix-expression \terminal{[} \opt{expression-list} \terminal{]}
33043304
\end{ncsimplebnf}
33053305
the operator function
33063306
is selected by overload resolution\iref{over.match.oper}.
33073307
If a member function is selected,
33083308
the expression is interpreted as
33093309
\begin{ncsimplebnf}
3310-
postfix-expression . \keyword{operator} \terminal{[}\terminal{]} \terminal{(} expr-or-braced-init-list \terminal{)}
3310+
postfix-expression . \keyword{operator} \terminal{[}\terminal{]} \terminal{(} \opt{expression-list} \terminal{)}
33113311
\end{ncsimplebnf}
33123312

33133313
\pnum
33143314
\begin{example}
33153315
\begin{codeblock}
33163316
struct X {
33173317
Z operator[](std::initializer_list<int>);
3318+
Z oeprator[](auto...);
33183319
};
33193320
X x;
33203321
x[{1,2,3}] = 7; // OK: meaning \tcode{x.\keyword{operator}[](\{1,2,3\})}
3322+
x[1,2,3] = 7; // OK: meaning \tcode{x.\keyword{operator}[](1,2,3)}
33213323
int a[10];
33223324
a[{1,2,3}] = 7; // error: built-in subscript operator
3325+
a[1,2,3] = 7; // error: built-in subscript operator
33233326
\end{codeblock}
33243327
\end{example}
33253328

source/preprocessor.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,7 @@
17771777
\defnxname{cpp_inline_variables} & \tcode{201606L} \\ \rowsep
17781778
\defnxname{cpp_lambdas} & \tcode{200907L} \\ \rowsep
17791779
\defnxname{cpp_modules} & \tcode{201907L} \\ \rowsep
1780+
\defnxname{cpp_multidimensional_subscript} & \tcode{202110L} \\ \rowsep
17801781
\defnxname{cpp_namespace_attributes} & \tcode{201411L} \\ \rowsep
17811782
\defnxname{cpp_noexcept_function_type} & \tcode{201510L} \\ \rowsep
17821783
\defnxname{cpp_nontype_template_args} & \tcode{201911L} \\ \rowsep

source/xrefdelta.tex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,8 @@
6969
\movedxref{range.split.outer.value}{range.lazy.split.outer.value}
7070
\movedxref{range.split.inner}{range.lazy.split.inner}
7171

72+
% P2128R6 Multidimensional subscript operator
73+
\removedxref{depr.comma.subscript}
74+
7275
% Deprecated features.
7376
%\deprxref{old.label} (if moved to depr.old.label, otherwise use \movedxref)

0 commit comments

Comments
 (0)