Skip to content

[expr.add] Clarify if/otherwise ladder #2315

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 9, 2018
Merged
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
45 changes: 21 additions & 24 deletions source/expressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5438,9 +5438,13 @@

\pnum
\indextext{arithmetic!pointer}%
When an expression that has integral type is added to or subtracted from
a pointer, the result has the type of the pointer operand.
If the expression \tcode{P} points to element $\mathtt{x[}i\mathtt{]}$
When an expression \tcode{J} that has integral type
is added to or subtracted from an expression \tcode{P} of pointer type,
the result has the type of \tcode{P}.
\begin{itemize}
\item If \tcode{P} evaluates to a null pointer value and
\tcode{J} evaluates to 0, the result is a null pointer value.
\item Otherwise, if \tcode{P} points to element $\mathtt{x[}i\mathtt{]}$
of an array object \tcode{x} with $n$ elements,%
\footnote{An object that is not an array element is considered to belong to a
single-element array for this purpose; see~\ref{expr.unary.op}.
Expand All @@ -5450,39 +5454,39 @@
the expressions \tcode{P + J} and \tcode{J + P}
(where \tcode{J} has the value $j$)
point to the (possibly-hypothetical) element
$\mathtt{x[}i + j\mathtt{]}$ if $0 \le i + j \le n$;
otherwise, the behavior is undefined.
Likewise, the expression \tcode{P - J}
$\mathtt{x[}i + j\mathtt{]}$ if $0 \le i + j \le n$
and the expression \tcode{P - J}
points to the (possibly-hypothetical) element
$\mathtt{x[}i - j\mathtt{]}$ if $0 \le i - j \le n$;
otherwise, the behavior is undefined.
$\mathtt{x[}i - j\mathtt{]}$ if $0 \le i - j \le n$.
\item Otherwise, the behavior is undefined.
\end{itemize}

\pnum
\indextext{\idxcode{ptrdiff_t}!implementation-defined type of}%
\indextext{subtraction!implementation-defined pointer}%
\indextext{\idxcode{ptrdiff_t}}%
\indexhdr{cstddef}%
\indextext{comparison!undefined pointer}%
When two pointers to elements of the same array object are subtracted,
When two pointer expressions \tcode{P} and \tcode{Q} are subtracted,
the type of the result is an \impldef{type of \tcode{ptrdiff_t}} signed
integral type; this type shall be the same type that is defined as
\tcode{std::ptrdiff_t} in the \tcode{<cstddef>}
header\iref{support.types}.
If the expressions \tcode{P} and \tcode{Q}
point to, respectively,
elements
$\mathtt{x[}i\mathtt{]}$
and
$\mathtt{x[}j\mathtt{]}$
\begin{itemize}
\item If \tcode{P} and \tcode{Q} both evaluate to null pointer values,
the result is 0.
\item Otherwise, if \tcode{P} and \tcode{Q} point to, respectively,
elements $\mathtt{x[}i\mathtt{]}$ and $\mathtt{x[}j\mathtt{]}$
of the same array object \tcode{x},
the expression \tcode{P - Q} has the value $i - j$;
otherwise, the behavior is undefined.
the expression \tcode{P - Q} has the value $i - j$.
\item Otherwise, the behavior is undefined.
\begin{note}
If the value $i - j$
is not in the range of representable values
of type \tcode{std::ptrdiff_t},
the behavior is undefined.
\end{note}
\end{itemize}

\pnum
For addition or subtraction, if the expressions \tcode{P} or \tcode{Q} have
Expand All @@ -5492,13 +5496,6 @@
pointer arithmetic when the array contains objects of a derived class type.
\end{note}

\pnum
If the value 0 is added to or subtracted from a null pointer value,
the result is a null pointer value.
If two null pointer values are subtracted,
the result compares equal to the value 0
converted to the type \tcode{std::ptrdiff_t}.

\rSec2[expr.shift]{Shift operators}

\pnum
Expand Down