Skip to content

P2128R6 Multidimensional subscript operator #4997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions source/compatibility.tex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@
\end{codeblock}
\end{example}

\rSec2[diff.cpp20.expr]{\ref{expr}: expressions}

\diffref{expr.sub}
\change
Change the meaning of comma in subscript expressions.
\rationale
Enable repurposing a deprecated syntax to support multidimensional indexing.
\effect
Valid \CppXX{} code that uses a comma expression within a
subscript expression may fail to compile. For example:
\begin{codeblock}
arr[1, 2] // was equivalent to \tcode{arr[(1, 2)]},
// now equivalent to \tcode{arr.operator[](1, 2)} or ill-formed
\end{codeblock}

\rSec2[diff.cpp20.utilities]{\ref{utilities}: general utilities library}

\diffref{format}
Expand Down
38 changes: 16 additions & 22 deletions source/expressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2982,7 +2982,7 @@
\begin{bnf}
\nontermdef{postfix-expression}\br
primary-expression\br
postfix-expression \terminal{[} expr-or-braced-init-list \terminal{]}\br
postfix-expression \terminal{[} \opt{expression-list} \terminal{]}\br
postfix-expression \terminal{(} \opt{expression-list} \terminal{)}\br
simple-type-specifier \terminal{(} \opt{expression-list} \terminal{)}\br
typename-specifier \terminal{(} \opt{expression-list} \terminal{)}\br
Expand Down Expand Up @@ -3020,8 +3020,18 @@
\pnum
\indextext{operator!subscripting}%
\indextext{\idxcode{[]}|see{operator, subscripting}}%
A postfix expression followed by an expression in square brackets is a
postfix expression. One of the expressions shall be a glvalue of type ``array of
A \defnadj{subscript}{expression} is a postfix expression
followed by square brackets containing
a possibly empty, comma-separated list of \grammarterm{initializer-clause}s
which constitute the arguments to the subscript operator.
The \grammarterm{postfix-expression} is sequenced before
each expression in the \grammarterm{expression-list}.

\pnum
With the built-in subscript operator,
an \grammarterm{expression-list} shall be present,
consisting of a single \grammarterm{assignment-expression}.
One of the expressions shall be a glvalue of type ``array of
\tcode{T}'' or a prvalue of type ``pointer
to \tcode{T}'' and the other shall be a prvalue of unscoped enumeration or integral type.
The result is of type ``\tcode{T}''.
Expand All @@ -3036,13 +3046,6 @@
\tcode{*((E1)+(E2))},
except that in the case of an array operand, the result is an lvalue
if that operand is an lvalue and an xvalue otherwise.
The expression \tcode{E1} is sequenced before the expression \tcode{E2}.

\pnum
A comma expression\iref{expr.comma}
appearing as the \grammarterm{expr-or-braced-init-list}
of a subscripting expression is deprecated;
see \ref{depr.comma.subscript}.

\pnum
\begin{note}
Expand All @@ -3052,9 +3055,6 @@
\tcode{+} and~\ref{dcl.array} for details of array types.
\end{note}

\pnum
A \grammarterm{braced-init-list} shall not be used with the built-in subscript operator.

\rSec3[expr.call]{Function call}

\pnum
Expand Down Expand Up @@ -7067,7 +7067,9 @@
\pnum
\begin{note}
In contexts where the comma token is given special meaning
(e.g. function calls\iref{expr.call}, lists of initializers\iref{dcl.init},
(e.g. function calls\iref{expr.call},
subscript expressions\iref{expr.sub},
lists of initializers\iref{dcl.init},
or \grammarterm{template-argument-list}{s}\iref{temp.names}),
the comma operator as described in this subclause can appear only in parentheses.
\begin{example}
Expand All @@ -7079,14 +7081,6 @@
\end{example}
\end{note}

\pnum
\begin{note}
A comma expression
appearing as the \grammarterm{expr-or-braced-init-list}
of a subscripting expression\iref{expr.sub} is deprecated;
see \ref{depr.comma.subscript}.
\end{note}

\rSec1[expr.const]{Constant expressions}%
\indextext{expression!constant}

Expand Down
18 changes: 0 additions & 18 deletions source/future.tex
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,6 @@
\end{codeblock}
\end{example}

\rSec1[depr.comma.subscript]{Comma operator in subscript expressions}

\pnum
A comma expression\iref{expr.comma}
appearing as the \grammarterm{expr-or-braced-init-list}
of a subscripting expression\iref{expr.sub} is deprecated.
\begin{note}
A parenthesized comma expression is not deprecated.
\end{note}
\begin{example}
\begin{codeblock}
void f(int *a, int b, int c) {
a[b,c]; // deprecated
a[(b,c)]; // OK
}
\end{codeblock}
\end{example}

\rSec1[depr.array.comp]{Array comparisons}

\pnum
Expand Down
11 changes: 7 additions & 4 deletions source/overloading.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3297,29 +3297,32 @@
\pnum
A \defnadj{subscripting}{operator function}
is a function named \tcode{\keyword{operator}[]}
that is a non-static member function with exactly one non-object parameter.
that is a non-static member function.
For an expression of the form
\begin{ncsimplebnf}
postfix-expression \terminal{[} expr-or-braced-init-list \terminal{]}
postfix-expression \terminal{[} \opt{expression-list} \terminal{]}
\end{ncsimplebnf}
the operator function
is selected by overload resolution\iref{over.match.oper}.
If a member function is selected,
the expression is interpreted as
\begin{ncsimplebnf}
postfix-expression . \keyword{operator} \terminal{[}\terminal{]} \terminal{(} expr-or-braced-init-list \terminal{)}
postfix-expression . \keyword{operator} \terminal{[}\terminal{]} \terminal{(} \opt{expression-list} \terminal{)}
\end{ncsimplebnf}

\pnum
\begin{example}
\begin{codeblock}
struct X {
Z operator[](std::initializer_list<int>);
Z oeprator[](auto...);
};
X x;
x[{1,2,3}] = 7; // OK: meaning \tcode{x.\keyword{operator}[](\{1,2,3\})}
x[{1,2,3}] = 7; // OK, meaning \tcode{x.\keyword{operator}[](\{1,2,3\})}
x[1,2,3] = 7; // OK, meaning \tcode{x.\keyword{operator}[](1,2,3)}
int a[10];
a[{1,2,3}] = 7; // error: built-in subscript operator
a[1,2,3] = 7; // error: built-in subscript operator
\end{codeblock}
\end{example}

Expand Down
1 change: 1 addition & 0 deletions source/preprocessor.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,7 @@
\defnxname{cpp_inline_variables} & \tcode{201606L} \\ \rowsep
\defnxname{cpp_lambdas} & \tcode{200907L} \\ \rowsep
\defnxname{cpp_modules} & \tcode{201907L} \\ \rowsep
\defnxname{cpp_multidimensional_subscript} & \tcode{202110L} \\ \rowsep
\defnxname{cpp_namespace_attributes} & \tcode{201411L} \\ \rowsep
\defnxname{cpp_noexcept_function_type} & \tcode{201510L} \\ \rowsep
\defnxname{cpp_nontype_template_args} & \tcode{201911L} \\ \rowsep
Expand Down
3 changes: 3 additions & 0 deletions source/xrefdelta.tex
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@
\movedxref{range.split.outer.value}{range.lazy.split.outer.value}
\movedxref{range.split.inner}{range.lazy.split.inner}

% P2128R6 Multidimensional subscript operator
\removedxref{depr.comma.subscript}

% Deprecated features.
%\deprxref{old.label} (if moved to depr.old.label, otherwise use \movedxref)