Skip to content

Commit

Permalink
[expr.add] Move note, add example (#4778)
Browse files Browse the repository at this point in the history
The original note wasn't quite in the right place.
It is moved and made more concrete.

In the original place of the note we now put
a more immediately relevant example.
  • Loading branch information
languagelawyer committed Sep 16, 2022
1 parent 1383e97 commit c70087a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions source/expressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6234,6 +6234,11 @@
$i - j$ of \tcode{x} if $0 \le i - j \le n$.
\item Otherwise, the behavior is undefined.
\end{itemize}
\begin{note}
Adding a value other than $0$ or $1$
to a pointer to a base class subobject, a member subobject,
or a complete object results in undefined behavior.
\end{note}

\pnum
\indextext{\idxcode{ptrdiff_t}!implementation-defined type of}%
Expand Down Expand Up @@ -6265,10 +6270,14 @@
For addition or subtraction, if the expressions \tcode{P} or \tcode{Q} have
type ``pointer to \cv{}~\tcode{T}'', where \tcode{T} and the array element type
are not similar\iref{conv.qual}, the behavior is undefined.
\begin{note}
In particular, a pointer to a base class cannot be used for
pointer arithmetic when the array contains objects of a derived class type.
\end{note}
\begin{example}
\begin{codeblock}
int arr[5] = {1, 2, 3, 4, 5};
unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1);
unsigned int k = *p; // OK, value of \tcode{k} is $2$\iref{conv.lval}
unsigned int *q = p + 1; // undefined behavior: \tcode{p} points to an \tcode{int}, not an \tcode{unsigned int} object
\end{codeblock}
\end{example}

\rSec2[expr.shift]{Shift operators}

Expand Down

0 comments on commit c70087a

Please sign in to comment.