Skip to content

[conv.lval] Determine the value of a constant without accessing an object #4495

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 36 additions & 27 deletions source/expressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@
\pnum
\indextext{conversion!lvalue-to-rvalue}%
\indextext{type!incomplete}%
A glvalue\iref{basic.lval} of a non-function, non-array type \tcode{T}
A glvalue\iref{basic.lval} $E$ of a non-function, non-array type \tcode{T}
can be converted to
a prvalue.
\begin{footnote}
Expand All @@ -618,38 +618,21 @@
cv-qualified types.
\end{footnote}

\pnum
When an lvalue-to-rvalue conversion
is applied to an expression $E$, and either
\begin{itemize}
\item $E$ is not potentially evaluated, or
\item the evaluation of $E$ results in the evaluation of a member
$E_\tcode{x}$ of the set of potential results of $E$, and $E_\tcode{x}$
names a variable \tcode{x} that is not odr-used by
$E_\tcode{x}$\iref{basic.def.odr},
\end{itemize}
the value contained in the referenced object is not accessed.
\begin{example}
\begin{codeblock}
struct S { int n; };
auto f() {
S x { 1 };
constexpr S y { 2 };
return [&](bool b) { return (b ? y : x).n; };
}
auto g = f();
int m = g(false); // undefined behavior: access of \tcode{x.n} outside its lifetime
int n = g(true); // OK, does not access \tcode{y.n}
\end{codeblock}
\end{example}

\pnum
The result of the conversion is determined according to the
following rules:

\begin{itemize}

\item If \tcode{T} is \cv{}~\tcode{std::nullptr_t}, the result is a
\item If $E$ is not potentially evaluated,
the result is an unspecified value of type \tcode{T}.
\begin{note}
An expression that is not potentially evaluated is only
analyzed for its type or other expression properties,
thus its value is not relevant.
\end{note}

\item Otherwise, if \tcode{T} is \cv{}~\tcode{std::nullptr_t}, the result is a
null pointer constant\iref{conv.ptr}.
\begin{note}
Since the conversion does not access the object to which the glvalue refers,
Expand All @@ -661,6 +644,17 @@
type, the conversion copy-initializes the result object from
the glvalue.

\item Otherwise,
if the evaluation of $E$ results in
the evaluation of a member $E_\tcode{x}$ of the set of potential results of $E$,
and $E_\tcode{x}$ names a variable \tcode{x}
that is not odr-used by $E_\tcode{x}$\iref{basic.def.odr},
the result is the value of \tcode{x}.
\begin{note}
The variable \tcode{x} is thus usable in constant expressions\iref{expr.const};
the conversion does not access the object to which the glvalue refers.
\end{note}

\item Otherwise, if the object to which the glvalue refers contains an invalid
pointer value~(\ref{basic.stc.dynamic.deallocation},
\ref{basic.stc.dynamic.safety}), the behavior is
Expand All @@ -675,6 +669,21 @@
See also~\ref{basic.lval}.
\end{note}

\pnum
\begin{example}
\begin{codeblock}
struct S { int n; };
auto f() {
S x { 1 };
constexpr S y { 2 };
return [&](bool b) { return (b ? y : x).n; };
}
auto g = f();
int m = g(false); // undefined behavior: access of \tcode{x.n} outside its lifetime
int n = g(true); // OK, does not access \tcode{y.n}
\end{codeblock}
\end{example}

\rSec2[conv.array]{Array-to-pointer conversion}

\pnum
Expand Down