(CA{}); // OK, temporary materialization conversion is performed
\end{codeblock}
\end{example}
\pnum
\begin{note}
Depending on the type of the object, a write operation through the
pointer, lvalue or pointer to data member resulting from a
\keyword{const_cast} that casts away a const-qualifier
\begin{footnote}
\keyword{const_cast}
is not limited to conversions that cast away a
const-qualifier.
\end{footnote}
can produce undefined behavior\iref{dcl.type.cv}.
\end{note}
\pnum
\indextext{\idxcode{const}!cast away}%
A conversion from a type \tcode{T1} to a type \tcode{T2}
\defnx{casts away constness}{casting away constness}
if \tcode{T1} and \tcode{T2} are different,
there is a qualification-decomposition\iref{conv.qual} of \tcode{T1}
yielding \placeholder{n} such that
\tcode{T2} has a qualification-decomposition of the form
\begin{indented}
$\cv{}_0^2$ $P_0^2$ $\cv{}_1^2$ $P_1^2$ $\cdots$ $\cv{}_{n-1}^2$ $P_{n-1}^2$ $\cv{}_n^2$ $\mathtt{U}_2$,
\end{indented}
and there is no qualification conversion that converts \tcode{T1} to
\begin{indented}
$\cv{}_0^2$ $P_0^1$ $\cv{}_1^2$ $P_1^1$ $\cdots$ $\cv{}_{n-1}^2$ $P_{n-1}^1$ $\cv{}_n^2$ $\mathtt{U}_1$.
\end{indented}
\pnum
Casting from an lvalue of type \tcode{T1} to an lvalue of type
\tcode{T2} using an lvalue reference cast
or casting from an expression of type \tcode{T1} to an xvalue of type \tcode{T2} using
an rvalue reference cast
casts away constness if a cast from a prvalue of type ``pointer to \tcode{T1}'' to the type ``pointer to
\tcode{T2}'' casts away constness.
\pnum
\begin{note}
Some conversions which involve only changes in cv-qualification cannot
be done using \keyword{const_cast}. For instance, conversions between
pointers to functions are not covered because such conversions lead to
values whose use causes undefined behavior. For the same reasons,
conversions between pointers to member functions, and in particular, the
conversion from a pointer to a const member function to a pointer to a
non-const member function, are not covered.
\end{note}
\indextext{expression!postfix|)}
\rSec2[expr.unary]{Unary expressions}
\rSec3[expr.unary.general]{General}
\pnum
\indextext{expression!unary|(}%
Expressions with unary operators group right-to-left.
\indextext{expression!unary}%
\indextext{operator!unary}%
\indextext{operator!\idxcode{sizeof}}%
\indextext{operator!cast}%
%
\begin{bnf}
%% Ed. note: character protrusion would misalign operators.
\microtypesetup{protrusion=false}
\nontermdef{unary-expression}\br
postfix-expression\br
unary-operator cast-expression\br
\terminal{++} cast-expression\br
\terminal{--} cast-expression\br
await-expression\br
\keyword{sizeof} unary-expression\br
\keyword{sizeof} \terminal{(} type-id \terminal{)}\br
\keyword{sizeof} \terminal{...} \terminal{(} identifier \terminal{)}\br
\keyword{alignof} \terminal{(} type-id \terminal{)}\br
noexcept-expression\br
new-expression\br
delete-expression\br
reflect-expression
\end{bnf}
\indextext{operator!indirection}%
\indextext{\idxcode{*}|see{operator, indirection}}%
\indextext{operator!address-of}%
\indextext{\idxcode{\&}|see{operator, address-of}}%
\indextext{operator!unary minus}%
\indextext{\idxcode{-}|see{operator, unary minus}}%
\indextext{operator!unary plus}%
\indextext{\idxcode{+}|see{operator, unary plus}}%
\indextext{operator!logical negation}%
\indextext{\idxcode{"!}|see{operator, logical negation}}%
\indextext{operator!ones' complement}%
\indextext{~@\tcode{\~}|see{operator, ones' complement}}%
\indextext{operator!increment}%
\indextext{operator!decrement}%
%
\begin{bnf}
%% Ed. note: character protrusion would misalign operators.
\microtypesetup{protrusion=false}
\nontermdef{unary-operator} \textnormal{one of}\br
\terminal{* \& + - ! \~}
\end{bnf}
\rSec3[expr.unary.op]{Unary operators}
\pnum
\indextext{expression!unary operator}%
\indextext{operator!unary}%
The unary \tcode{*} operator performs \defn{indirection}.
\indextext{dereferencing|see{indirection}}%
Its operand shall be a prvalue of type ``pointer to \tcode{T}'',
where \tcode{T} is an object or function type.
The operator yields an lvalue of type \tcode{T}.
If the operand points to an object or function,
the result denotes that object or function;
otherwise, the behavior is undefined except as specified in \ref{expr.typeid}.
\begin{note}
Indirection through a pointer to an out-of-lifetime object is valid\iref{basic.life}.
\end{note}
\begin{note}
\indextext{type!incomplete}%
Indirection through a pointer to an incomplete type (other than
\cv{} \keyword{void}) is valid. The lvalue thus obtained can be
used in limited ways (to initialize a reference, for example); this
lvalue must not be converted to a prvalue, see~\ref{conv.lval}.
\end{note}
\pnum
Each of the following unary operators yields a prvalue.
\pnum
\indextext{name!address of cv-qualified}%
\indextext{expression!pointer-to-member constant}%
The operand of the unary \tcode{\&} operator
shall be an lvalue of some type \tcode{T}.
\begin{itemize}
\item
If the operand is a \grammarterm{qualified-id} or \grammarterm{splice-expression}
designating a non-static member \tcode{m},
other than an explicit object member function,
\tcode{m} shall be a direct member of some class \tcode{C}
that is not an anonymous union.
The result has type ``pointer to member of class \tcode{C} of type \tcode{T}''
and designates \tcode{C::m}.
\begin{note}
A \grammarterm{qualified-id}
that names a member of a namespace-scope anonymous union
is considered to be a class member access expression\iref{expr.prim.id.general}
and cannot be used to form a pointer to member.
\end{note}
\item
Otherwise, the result has type ``pointer to \tcode{T}'' and points to
the designated object\iref{intro.memory} or function\iref{basic.compound}.
If the operand designates an explicit object member function\iref{dcl.fct},
the operand shall be
a \grammarterm{qualified-id} or a \grammarterm{splice-expression}.
\begin{note}
In particular, taking the address of a variable of type ``\cv{}~\tcode{T}''
yields a pointer of type ``pointer to \cv{}~\tcode{T}''.
\end{note}
\end{itemize}
\begin{example}
\begin{codeblock}
struct A { int i; };
struct B : A { };
... &B::i ... // has type \tcode{int A::*}
int a;
int* p1 = &a;
int* p2 = p1 + 1; // defined behavior
bool b = p2 > p1; // defined behavior, with value \tcode{true}
\end{codeblock}
\end{example}
\begin{note}
A pointer to member formed from a \keyword{mutable} non-static data
member\iref{dcl.stc} does not reflect the \keyword{mutable} specifier
associated with the non-static data member.
\end{note}
\pnum
A pointer to member is only formed when an explicit \tcode{\&} is used
and its operand is
a \grammarterm{qualified-id} or \grammarterm{splice-expression}
not enclosed in parentheses.
\begin{note}
That is, the expression \tcode{\&(qualified-id)}, where the
\grammarterm{qualified-id} is enclosed in parentheses, does not form an
expression of type ``pointer to member''. Neither does
\tcode{qualified-id}, because there is no implicit conversion from a
\grammarterm{qualified-id} for a non-static member function to the type
``pointer to member function'' as there is from an lvalue of function
type to the type ``pointer to function''\iref{conv.func}. Nor is
\tcode{\&unqualified-id} a pointer to member, even within the scope of
the \grammarterm{unqualified-id}'s class.
\end{note}
\pnum
If \tcode{\&} is applied to an lvalue of incomplete class type and the
complete type declares \tcode{\keyword{operator}\&()}, it is unspecified whether
the operator has the built-in meaning or the operator function is
called. The operand of \tcode{\&} shall not be a bit-field.
\pnum
\indextext{overloaded function!address of}%
\begin{note}
The address of an overload set\iref{over} can be taken
only in a context that uniquely determines
which function is referred to (see~\ref{over.over}).
Since the context can affect whether the operand is a static or
non-static member function, the context can also affect whether the
expression has type ``pointer to function'' or ``pointer to member
function''.
\end{note}
\pnum
\indextext{operator!unary plus}%
The operand of the unary \tcode{+} operator shall be a prvalue of
arithmetic, unscoped
enumeration, or pointer type and the result is the value of the
argument. Integral promotion is performed on integral or enumeration
operands. The type of the result is the type of the promoted operand.
\pnum
\indextext{operator!unary minus}%
The operand of the unary \tcode{-} operator shall be a prvalue of
arithmetic or unscoped
enumeration type and the result is the negative of its operand. Integral
promotion is performed on integral or enumeration operands. The negative
of an unsigned quantity is computed by subtracting its value from $2^n$,
where $n$ is the number of bits in the promoted operand. The type of the
result is the type of the promoted operand.
\begin{note}
The result is the two's complement of the operand
(where operand and result are considered as unsigned).
\end{note}
\pnum
\indextext{operator!logical negation}%
The operand of the logical negation operator \tcode{!} is contextually
converted to \keyword{bool}\iref{conv};
its value is \keyword{true}
if the converted operand is \keyword{false} and \keyword{false} otherwise.
The type of the result is \keyword{bool}.
\pnum
\indextext{operator!ones' complement}%
The operand of the \tcode{\~{}} operator shall be a prvalue of
integral or unscoped enumeration type.
Integral promotions are performed.
The type of the result is the type of the promoted operand.
% FIXME: [basic.fundamental]/p5 uses $x_i$; [expr] uses $\tcode{x}_i$.
Given the coefficients $\tcode{x}_i$
of the base-2 representation\iref{basic.fundamental}
of the promoted operand \tcode{x},
the coefficient $\tcode{r}_i$
of the base-2 representation of the result \tcode{r}
is 1 if $\tcode{x}_i$ is 0, and 0 otherwise.
\begin{note}
The result is the ones' complement of the operand
(where operand and result are considered as unsigned).
\end{note}
There is an ambiguity
in the grammar when \tcode{\~{}} is followed by
a \grammarterm{type-name} or \grammarterm{computed-type-specifier}.
The ambiguity is resolved by treating \tcode{\~{}} as the
operator rather than as the start of an \grammarterm{unqualified-id}
naming a destructor.
\begin{note}
Because the grammar does not permit an operator to follow the
\tcode{.}, \tcode{->}, or \tcode{::} tokens, a \tcode{\~{}} followed by
a \grammarterm{type-name} or \grammarterm{computed-type-specifier} in a
member access expression or \grammarterm{qualified-id} is
unambiguously parsed as a destructor name.
\end{note}
\rSec3[expr.pre.incr]{Increment and decrement}
\pnum
\indextext{expression!increment}%
\indextext{expression!decrement}%
\indextext{operator!increment}%
\indextext{operator!decrement}%
\indextext{prefix \tcode{++}}%
\indextext{prefix \tcode{--}}%
The operand of prefix \tcode{++} or \tcode{--}
shall not be of type \cv{}~\tcode{bool}.
An operand with volatile-qualified type is deprecated;
see~\ref{depr.volatile.type}.
The expression \tcode{++x} is otherwise equivalent to \tcode{x+=1} and
the expression \tcode{--x} is otherwise equivalent to \tcode{x-=1}\iref{expr.assign}.
\begin{note}
For postfix increment and decrement, see~\ref{expr.post.incr}.
\end{note}
\rSec3[expr.await]{Await}
\indextext{expression!await}%
\indextext{\idxcode{co_await}}%
\pnum
The \keyword{co_await} expression is used to suspend evaluation of a
coroutine\iref{dcl.fct.def.coroutine} while awaiting completion of
the computation represented by the operand expression.
Suspending the evaluation of a coroutine
transfers control to its caller or resumer.
\begin{bnf}
\nontermdef{await-expression}\br
\keyword{co_await} cast-expression
\end{bnf}
\pnum
An \grammarterm{await-expression} shall appear only as a potentially-evaluated
expression within the \grammarterm{compound-statement} of a
\grammarterm{function-body} or \grammarterm{lambda-expression},
in either case
outside of a \grammarterm{handler}\iref{except.pre}.
In a \grammarterm{declaration-statement} or in the
\grammarterm{simple-declaration} (if any)
of an \grammarterm{init-statement}, an \grammarterm{await-expression}
shall appear only in an \grammarterm{initializer} of that
\grammarterm{declaration-statement} or \grammarterm{simple-declaration}.
An \grammarterm{await-expression} shall not appear in a
default argument\iref{dcl.fct.default}.
An \grammarterm{await-expression} shall not appear in the initializer of
a block variable with static or thread storage duration.
An \grammarterm{await-expression} shall not be
a potentially-evaluated subexpression
of the predicate of a contract assertion\iref{basic.contract}.
A context within a function where an \grammarterm{await-expression} can appear
is called a \term{suspension context} of the function.
\pnum
Evaluation of an \grammarterm{await-expression} involves the following
auxiliary types, expressions, and objects:
\begin{itemize}
\item
\placeholder{p} is an lvalue naming the promise
object\iref{dcl.fct.def.coroutine}
of the enclosing coroutine and \tcode{P} is the type of that object.
\item Unless
the \grammarterm{await-expression} was implicitly produced by
a \grammarterm{yield-expression}\iref{expr.yield},
an initial await expression,
or a final await expression\iref{dcl.fct.def.coroutine},
a search is performed for the name \tcode{await_transform}
in the scope of \tcode{P}\iref{class.member.lookup}.
If this search is performed and finds at least one declaration,
then \placeholder{a} is
\mbox{\placeholder{p}\tcode{.await_transform(}\grammarterm{cast-expression}\tcode{)}};
otherwise, \placeholder{a} is the \grammarterm{cast-expression}.
\item
\placeholder{o} is determined by enumerating the applicable
\tcode{\keyword{operator} \keyword{co_await}} functions for an argument
\placeholder{a}\iref{over.match.oper}, and choosing the best one through
overload resolution\iref{over.match}. If overload resolution is ambiguous,
the program is ill-formed.
If no viable functions are found, \placeholder{o} is \placeholder{a}.
Otherwise, \placeholder{o} is a call to the selected function
with the argument \placeholder{a}.
If \placeholder{o} would be a prvalue,
the temporary materialization conversion\iref{conv.rval} is applied.
\item
\placeholder{e} is an lvalue
referring to the result of evaluating
the (possibly-converted) \placeholder{o}.
\item
% FIXME: h needs to be an expression so we can use it as an argument
% to await_suspend. What should its value category be?
% Don't forget to remove "and objects" from the intro sentence when
% this is fixed.
\placeholder{h} is an object of type
\tcode{std::coroutine_handle}
referring to the enclosing coroutine.
\item
\placeholder{await-ready} is the expression
\placeholder{e}\tcode{.await_ready()},
contextually converted to \tcode{bool}.
\item
\placeholder{await-suspend} is the expression
\placeholder{e}\tcode{.await_suspend(}\placeholder{h}\tcode{)},
which shall be a prvalue of type \keyword{void}, \keyword{bool}, or
\tcode{std::coroutine_handle} for some type \tcode{Z}.
\item
\placeholder{await-resume} is the expression
\placeholder{e}\tcode{.await_resume()}.
\end{itemize}
\pnum
The \grammarterm{await-expression} has the same type and value category
as the \placeholder{await-resume} expression.
\pnum
The \grammarterm{await-expression} evaluates
the (possibly-converted) \placeholder{o} expression and
the \placeholder{await-ready} expression, then:
\begin{itemize}
\item
If the result of \placeholder{await-ready} is \keyword{false},
the coroutine is considered suspended.
Then:
\begin{itemize}
\item
If the type of \placeholder{await-suspend}
is \tcode{std::coroutine_handle},
\placeholder{await-suspend}\tcode{.resume()} is evaluated.
\begin{note}
This resumes the coroutine referred to
by the result of \placeholder{await-suspend}.
Any number of coroutines can be successively resumed in this fashion,
eventually returning control flow to the current coroutine caller or
resumer\iref{dcl.fct.def.coroutine}.
\end{note}
\item
Otherwise, if the type of \placeholder{await-suspend}
is \keyword{bool},
\placeholder{await-suspend} is evaluated,
and the coroutine is resumed if the result is \keyword{false}.
\item
Otherwise, \placeholder{await-suspend} is evaluated.
\end{itemize}
If the evaluation of \placeholder{await-suspend}
exits via an exception, the exception is caught,
the coroutine is resumed, and the exception is immediately
rethrown\iref{except.throw}. Otherwise, control flow returns
to the current coroutine caller or resumer\iref{dcl.fct.def.coroutine}
without exiting any scopes\iref{stmt.jump}.
\indextext{coroutine!suspend point}%
The point in the coroutine
immediately prior to control returning to its caller or resumer
is a coroutine \defn{suspend point}.
\item
If the result of \placeholder{await-ready} is \keyword{true},
or when the coroutine is resumed
other than by rethrowing an exception from \placeholder{await-suspend},
the \placeholder{await-resume} expression is evaluated, and
its result is the result of the \grammarterm{await-expression}.
\end{itemize}
\begin{note}
With respect to sequencing,
an \grammarterm{await-expression} is indivisible\iref{intro.execution}.
\end{note}
\pnum
\begin{example}
\begin{codeblock}
template
struct my_future {
@\commentellip@
bool await_ready();
void await_suspend(std::coroutine_handle<>);
T await_resume();
};
template
auto operator co_await(std::chrono::duration d) {
struct awaiter {
std::chrono::system_clock::duration duration;
@\commentellip@
awaiter(std::chrono::system_clock::duration d) : duration(d) {}
bool await_ready() const { return duration.count() <= 0; }
void await_resume() {}
void await_suspend(std::coroutine_handle<> h) { @\commentellip@ }
};
return awaiter{d};
}
using namespace std::chrono;
my_future h();
my_future g() {
std::cout << "just about to go to sleep...\n";
co_await 10ms;
std::cout << "resumed\n";
co_await h();
}
auto f(int x = co_await h()); // error: \grammarterm{await-expression} outside of function suspension context
int a[] = { co_await h() }; // error: \grammarterm{await-expression} outside of function suspension context
\end{codeblock}
\end{example}
\rSec3[expr.sizeof]{Sizeof}
\pnum
\indextext{expression!\idxcode{sizeof}}%
\indextext{operator!\idxcode{sizeof}}%
\indextext{byte}%
The \keyword{sizeof} operator yields the number of bytes
occupied by a non-potentially-overlapping object of the type
of its operand. The operand is either an expression,
which is an unevaluated operand\iref{term.unevaluated.operand}, or a parenthesized
\grammarterm{type-id}.
\indextext{type!incomplete}%
The \keyword{sizeof} operator shall not be applied to an expression that
has function or incomplete type,
to the parenthesized name of such
types, or to a glvalue that designates a bit-field.
The result of \keyword{sizeof}
applied to any of the narrow character types is \tcode{1}.
The result of
\keyword{sizeof} applied to any other fundamental
type\iref{basic.fundamental} is \impldef{\tcode{sizeof} applied to
fundamental types
other than \tcode{char}, \tcode{signed char}, and \tcode{unsigned char}}.
\begin{note}
In particular, the values of \tcode{\keyword{sizeof}(\keyword{bool})}, \tcode{\keyword{sizeof}(\keyword{char16_t})},
\tcode{\keyword{sizeof}(\keyword{char32_t})}, and \tcode{\keyword{sizeof}(\keyword{wchar_t})} are
implementation-defined.
\begin{footnote}
\tcode{\keyword{sizeof}(\keyword{bool})} is not required to be \tcode{1}.
\end{footnote}
\end{note}
\begin{note}
See~\ref{intro.memory} for the definition of byte
and~\ref{term.object.representation} for the definition of object representation.
\end{note}
\pnum
\indextext{reference!\idxcode{sizeof}}%
When applied to a reference type, the result is the size
of the referenced type.
\indextext{class object!\idxcode{sizeof}}%
When applied to a class, the result is the number of bytes in an object
of that class including any padding required for placing objects of that
type in an array.
The result of applying \keyword{sizeof} to a
potentially-overlapping subobject is
the size of the type, not the size of the subobject.
\begin{footnote}
The actual size of a potentially-overlapping subobject
can be less than the result of
applying \keyword{sizeof} to the subobject, due to virtual base classes
and less strict padding requirements on potentially-overlapping subobjects.
\end{footnote}
\indextext{array!\idxcode{sizeof}}%
When applied to an array, the result is the total number of bytes in the
array. This implies that the size of an array of $n$ elements is
$n$ times the size of an element.
\pnum
The lvalue-to-rvalue\iref{conv.lval},
array-to-pointer\iref{conv.array}, and
function-to-pointer\iref{conv.func} standard conversions are not
applied to the operand of \keyword{sizeof}.
If the operand is a prvalue,
the temporary materialization conversion\iref{conv.rval}
is applied.
\pnum
The \grammarterm{identifier} in a \tcode{\keyword{sizeof}...} expression shall name a
pack. The \tcode{\keyword{sizeof}...} operator yields the number of elements
in the pack\iref{temp.variadic}.
A \tcode{\keyword{sizeof}...} expression is a pack expansion\iref{temp.variadic}.
\begin{example}
\begin{codeblock}
template
struct count {
static constexpr std::size_t value = sizeof...(Types);
};
\end{codeblock}
\end{example}
\pnum
\indextext{\idxcode{size_t}}%
\indexlibraryglobal{size_t}%
The result of \keyword{sizeof} and \tcode{\keyword{sizeof}...} is a prvalue of type
\tcode{std::size_t}.
\begin{note}
A \keyword{sizeof} expression
is an integral constant expression\iref{expr.const}.
The \grammarterm{typedef-name} \tcode{std::size_t} is declared in the standard header
\libheader{cstddef}\iref{cstddef.syn,support.types.layout}.
\end{note}
\rSec3[expr.alignof]{Alignof}
\pnum
\indextext{\idxcode{alignof}}%
\indextext{expression!\idxcode{alignof}}%
An \keyword{alignof} expression yields the alignment requirement
of its operand type. The operand shall be a \grammarterm{type-id}
representing a complete object type, or an array thereof, or a reference
to one of those types.
\pnum
The result is a prvalue of type \tcode{std::size_t}.
\begin{note}
An \keyword{alignof} expression
is an integral constant expression\iref{expr.const}.
The \grammarterm{typedef-name} \tcode{std::size_t} is declared in the standard header
\libheader{cstddef}\iref{cstddef.syn,support.types.layout}.
\end{note}
\pnum
When \keyword{alignof} is applied to a reference type, the result
is the alignment of the referenced type. When \keyword{alignof}
is applied to an array type, the result is the alignment of the
element type.
\rSec3[expr.unary.noexcept]{\tcode{noexcept} operator}
\indextext{\idxcode{noexcept}}%
\indextext{expression!\idxcode{noexcept}}%
\begin{bnf}
\nontermdef{noexcept-expression}\br
\keyword{noexcept} \terminal{(} expression \terminal{)}
\end{bnf}
\pnum
The operand of the \keyword{noexcept} operator
is an unevaluated operand\iref{term.unevaluated.operand}.
If the operand is a prvalue,
the temporary materialization conversion\iref{conv.rval} is applied.
\pnum
The result of the \keyword{noexcept} operator is a prvalue of type \keyword{bool}.
The result is \tcode{false} if
the full-expression of the operand is potentially-throwing\iref{except.spec}, and
\tcode{true} otherwise.
\begin{note}
A \grammarterm{noexcept-expression}
is an integral constant expression\iref{expr.const}.
\end{note}
\indextext{expression!unary|)}
\rSec3[expr.new]{New}
\pnum
\indextext{expression!\idxcode{new}}%
\indextext{free store|seealso{\tcode{new}}}%
\indextext{free store|seealso{\tcode{delete}}}%
\indextext{memory management|see{\tcode{new}}}%
\indextext{memory management|see{\tcode{delete}}}%
\indextext{storage management|see{\tcode{new}}}%
\indextext{storage management|see{\tcode{delete}}}%
\indextext{\idxcode{new}}%
The \grammarterm{new-expression} attempts to create an object of the
\grammarterm{type-id} or \grammarterm{new-type-id}\iref{dcl.name} to which
it is applied. The type of that object is the \defnadj{allocated}{type}.
\indextext{type!incomplete}%
This type shall be a complete object type\iref{term.incomplete.type},
but not an abstract class type\iref{class.abstract} or array
thereof\iref{intro.object}.
\begin{note}
Because references are not objects, references cannot be created by
\grammarterm{new-expression}{s}.
\end{note}
\begin{note}
The \grammarterm{type-id} can be a cv-qualified type, in which case the
object created by the \grammarterm{new-expression} has a cv-qualified type.
\end{note}
\begin{bnf}
\nontermdef{new-expression}\br
\opt{\terminal{::}} \keyword{new} \opt{new-placement} new-type-id \opt{new-initializer} \br
\opt{\terminal{::}} \keyword{new} \opt{new-placement} \terminal{(} type-id \terminal{)} \opt{new-initializer}
\end{bnf}
\indextext{\idxcode{new}!storage allocation}%
%
\begin{bnf}
\nontermdef{new-placement}\br
\terminal{(} expression-list \terminal{)}
\end{bnf}
\begin{bnf}
\nontermdef{new-type-id}\br
type-specifier-seq \opt{new-declarator}
\end{bnf}
\begin{bnf}
\nontermdef{new-declarator}\br
ptr-operator \opt{new-declarator} \br
noptr-new-declarator
\end{bnf}
\begin{bnf}
\nontermdef{noptr-new-declarator}\br
\terminal{[} \opt{expression} \terminal{]} \opt{attribute-specifier-seq}\br
noptr-new-declarator \terminal{[} constant-expression \terminal{]} \opt{attribute-specifier-seq}
\end{bnf}
\begin{bnf}
\nontermdef{new-initializer}\br
\terminal{(} \opt{expression-list} \terminal{)}\br
braced-init-list
\end{bnf}
\pnum
If a placeholder type\iref{dcl.spec.auto} or
a placeholder for a deduced class type\iref{dcl.type.class.deduct}
appears in the
\grammarterm{type-specifier-seq} of a \grammarterm{new-type-id} or
\grammarterm{type-id} of a \grammarterm{new-expression},
the allocated type is deduced as follows:
Let
\placeholder{init} be the \grammarterm{new-initializer}, if any,
and
\tcode{T} be the \grammarterm{new-type-id} or \grammarterm{type-id} of
the \grammarterm{new-expression}, then the allocated type is the type
deduced for the variable \tcode{x} in the invented
declaration\iref{dcl.spec.auto}:
\begin{codeblock}
T x @\textrm{\placeholder{init}}@ ;
\end{codeblock}
\begin{example}
\begin{codeblock}
new auto(1); // allocated type is \tcode{int}
auto x = new auto('a'); // allocated type is \tcode{char}, \tcode{x} is of type \tcode{char*}
template struct A { A(T, T); };
auto y = new A{1, 2}; // allocated type is \tcode{A}
\end{codeblock}
\end{example}
\pnum
The \grammarterm{new-type-id} in a \grammarterm{new-expression} is the longest
possible sequence of \grammarterm{new-declarator}{s}.
\begin{note}
This prevents ambiguities between the declarator operators \tcode{\&}, \tcode{\&\&},
\tcode{*}, and \tcode{[]} and their expression counterparts.
\end{note}
\begin{example}
\begin{codeblock}
new int * i; // syntax error: parsed as \tcode{(new int*) i}, not as \tcode{(new int)*i}
\end{codeblock}
The \tcode{*} is the pointer declarator and not the multiplication
operator.
\end{example}
\pnum
\begin{note}
\indextext{ambiguity!parentheses and}%
Parentheses in a \grammarterm{new-type-id} of a \grammarterm{new-expression}
can have surprising effects.
\begin{example}
\begin{codeblock}
new int(*[10])(); // error
\end{codeblock}
is ill-formed because the binding is
\begin{codeblock}
(new int) (*[10])(); // error
\end{codeblock}
Instead, the explicitly parenthesized version of the \keyword{new}
operator can be used to create objects of compound
types\iref{basic.compound}:
\begin{codeblock}
new (int (*[10])());
\end{codeblock}
allocates an array of \tcode{10} pointers to functions (taking no
argument and returning \tcode{int}).
\end{example}
\end{note}
\pnum
The \grammarterm{attribute-specifier-seq} in a \grammarterm{noptr-new-declarator} appertains
to the associated array type.
\pnum
Every \grammarterm{constant-expression} in a
\grammarterm{noptr-new-declarator} shall be a converted constant
expression\iref{expr.const} of type \tcode{std::size_t} and
its value shall be greater than zero.
\begin{example}
Given the definition \tcode{int n = 42},
\tcode{new float[n][5]} is well-formed (because \tcode{n} is the
\grammarterm{expression} of a \grammarterm{noptr-new-declarator}), but
\tcode{new float[5][n]} is ill-formed (because \tcode{n} is not a
constant expression).
Furthermore,
\tcode{new float[0]} is well-formed
(because \tcode{0} is the \grammarterm{expression}
of a \grammarterm{noptr-new-declarator},
where a value of zero results in the allocation of an array with no elements),
but \tcode{new float[n][0]} is ill-formed
(because \tcode{0} is the \grammarterm{constant-expression}
of a \grammarterm{noptr-new-declarator},
where only values greater than zero are allowed).
\end{example}
\pnum
If the \grammarterm{type-id} or \grammarterm{new-type-id}
denotes an array type of unknown bound\iref{dcl.array},
the \grammarterm{new-initializer} shall not be omitted;
the allocated object is an array with \tcode{n} elements,
where \tcode{n} is determined from the number of initial elements
supplied in
the \grammarterm{new-initializer}\iref{dcl.init.aggr,dcl.init.string}.
\pnum
\indextext{\idxcode{new}}%
If the \grammarterm{expression} in a \grammarterm{noptr-new-declarator}
is present, it is implicitly converted to \tcode{std::size_t}.
\indextext{function!allocation}%
The value of the \grammarterm{expression} is invalid if
\begin{itemize}
\item
the expression is of non-class type and its value before converting to
\tcode{std::size_t} is less than zero;
\item
the expression is of class type and its value before application of the second
standard conversion\iref{over.ics.user}
\begin{footnote}
If the conversion function
returns a signed integer type, the second standard conversion converts to the
unsigned type \tcode{std::size_t} and thus thwarts any attempt to detect a
negative value afterwards.
\end{footnote}
is less than zero;
\item
its value is such that the size of the allocated object would exceed the
\impldef{maximum size of an allocated object} limit\iref{implimits}; or
\item
the \grammarterm{new-initializer} is a \grammarterm{braced-init-list} and the
number of array elements for which initializers are provided (including the
terminating \tcode{'\textbackslash 0'} in a \grammarterm{string-literal}\iref{lex.string}) exceeds the
number of elements to initialize.
\end{itemize}
If the value of the \grammarterm{expression} is invalid after converting to \tcode{std::size_t}:
\begin{itemize}
\item
if the \grammarterm{expression} is a potentially-evaluated core constant expression,
the program is ill-formed;
\item
otherwise, an allocation function is not called; instead
\begin{itemize}
\item
if the allocation function that would have been called
has a non-throwing exception specification\iref{except.spec},
the value of the \grammarterm{new-expression}
is the null pointer value of the required result type;
\item
otherwise, the \grammarterm{new-expression} terminates by throwing an
exception of a type that would match a handler\iref{except.handle} of type
\tcode{std::bad_array_new_length}\iref{new.badlength}.
\end{itemize}
\end{itemize}
When the value of the \grammarterm{expression} is zero, the allocation
function is called to allocate an array with no elements.
\pnum
If the allocated type is an array,
the \grammarterm{new-initializer} is a \grammarterm{braced-init-list}, and
the \grammarterm{expression}
is potentially-evaluated and not a core constant expression,
the semantic constraints of copy-initializing a hypothetical element of
the array from an empty initializer list
are checked\iref{dcl.init.list}.
\begin{note}
The array can contain more elements than there are
elements in the \grammarterm{braced-init-list},
requiring initialization of the remainder of the array elements from
an empty initializer list.
\end{note}
\pnum
\indextext{storage duration!dynamic}%
Objects created by a \grammarterm{new-expression} have dynamic storage
duration\iref{basic.stc.dynamic}.
\begin{note}
\indextext{\idxcode{new}!scoping and}%
The lifetime of such an object is not necessarily restricted to the
scope in which it is created.
\end{note}
\pnum
\indextext{array!\idxcode{new}}%
When the allocated type is ``array of \tcode{N} \tcode{T}''
(that is, the \grammarterm{noptr-new-declarator} syntax is used or the
\grammarterm{new-type-id} or \grammarterm{type-id} denotes an array type),
the \grammarterm{new-expression} yields a prvalue of type ``pointer to \tcode{T}''
that points to the initial element (if any) of the array.
Otherwise, let \tcode{T} be the allocated type;
the \grammarterm{new-expression}
is a prvalue of type ``pointer to T''
that points to the object created.
\begin{note}
Both \tcode{\keyword{new} \keyword{int}} and \tcode{\keyword{new} \keyword{int}[10]} have type \tcode{\keyword{int}*} and
the type of \tcode{\keyword{new} \keyword{int}[i][10]} is \tcode{\keyword{int} (*)[10]}.
\end{note}
\pnum
A \grammarterm{new-expression} may obtain storage for the object by calling an
allocation function\iref{basic.stc.dynamic.allocation}. If
the \grammarterm{new-expression} terminates by throwing an exception, it
may release storage by calling a deallocation
function\iref{basic.stc.dynamic.deallocation}. If the allocated type
is a non-array type, the allocation function's name is
\indextext{\idxcode{operator new}}%
\indextext{\idxcode{operator delete}}%
\tcode{\keyword{operator} \keyword{new}} and the deallocation function's name is
\tcode{\keyword{operator} \keyword{delete}}. If the allocated type is an array type, the
allocation function's name is
\indextext{\idxcode{operator new}}%
\indextext{\idxcode{operator delete}}%
\tcode{\keyword{operator} \keyword{new}[]}
and the deallocation function's name is
\tcode{\keyword{operator} \keyword{delete}[]}.
\begin{note}
An implementation is expected to provide default definitions for the global
allocation
functions\iref{basic.stc.dynamic,new.delete.single,new.delete.array}.
A \Cpp{} program can provide alternative definitions of
these functions\iref{replacement.functions} and/or class-specific
versions\iref{class.free}.
The set of allocation and deallocation functions that can be called
by a \grammarterm{new-expression}
can include functions that do not perform allocation or deallocation;
for example, see \ref{new.delete.placement}.
\end{note}
\pnum
\indextext{operator!scope resolution}%
If the \grammarterm{new-expression}
does not begin with a unary \tcode{::} operator and
the allocated type is a class type \tcode{T} or array thereof,
a search is performed for the allocation function's name in the scope
of \tcode{T}\iref{class.member.lookup}.
Otherwise, or if nothing is found,
the allocation function's name is looked up by
searching for it in the global scope.
\pnum
An implementation is allowed to omit a call to a replaceable global allocation
function\iref{new.delete.single,new.delete.array}. When it does so,
the storage is instead provided by the implementation or provided by extending
the allocation of another \grammarterm{new-expression}.
\pnum
During an evaluation of a constant expression,
a call to a replaceable allocation function is always omitted\iref{expr.const}.
\pnum
The implementation may
extend the allocation of a \grammarterm{new-expression} \tcode{e1} to provide
storage for a \grammarterm{new-expression} \tcode{e2} if the
following would be true were the allocation not extended:
\begin{itemize}
\item the evaluation of \tcode{e1} is sequenced before the evaluation of
\tcode{e2}, and
\item \tcode{e2} is evaluated whenever \tcode{e1} obtains storage, and
\item both \tcode{e1} and \tcode{e2} invoke the same replaceable global
allocation function, and
\item if the allocation function invoked by \tcode{e1} and \tcode{e2} is
throwing, any exceptions thrown in the evaluation of either \tcode{e1} or
\tcode{e2} would be first caught in the same handler, and
\item the pointer values produced by \tcode{e1} and \tcode{e2} are operands to
evaluated \grammarterm{delete-expression}{s}, and
\item the evaluation of \tcode{e2} is sequenced before the evaluation of the
\grammarterm{delete-expression} whose operand is the pointer value produced
by \tcode{e1}.
\end{itemize}
\begin{example}
\begin{codeblock}
void can_merge(int x) {
// These allocations are safe for merging:
std::unique_ptr a{new (std::nothrow) char[8]};
std::unique_ptr b{new (std::nothrow) char[8]};
std::unique_ptr c{new (std::nothrow) char[x]};
g(a.get(), b.get(), c.get());
}
void cannot_merge(int x) {
std::unique_ptr a{new char[8]};
try {
// Merging this allocation would change its catch handler.
std::unique_ptr b{new char[x]};
} catch (const std::bad_alloc& e) {
std::cerr << "Allocation failed: " << e.what() << std::endl;
throw;
}
}
\end{codeblock}
\end{example}
\pnum
When a \grammarterm{new-expression} calls an allocation function and that
allocation has not been extended, the
\grammarterm{new-expression} passes the amount of space requested to the
allocation function as the first argument of type
\tcode{std::size_t}. That argument shall be no less than the size
of the object being created; it may be greater than the size of the
object being created only if the object is an array and
the allocation function is not a non-allocating form\iref{new.delete.placement}.
For arrays of
\keyword{char}, \tcode{\keyword{unsigned} \keyword{char}}, and \tcode{std::byte},
the difference between the
result of the \grammarterm{new-expression} and the address returned by the
allocation function shall be an integral multiple of the
strictest fundamental
alignment requirement\iref{basic.align} of any object type whose size
is no greater than the size of the array being created.
\begin{note}
\indextext{allocation!alignment storage}%
Because allocation functions are assumed to return pointers to storage
that is appropriately aligned for objects of any type
with fundamental alignment, this constraint
on array allocation overhead permits the common idiom of allocating
character arrays into which objects of other types will later be placed.
\end{note}
\pnum
When a \grammarterm{new-expression} calls an allocation function and that
allocation has been extended, the size argument to the allocation call shall
be no greater than the sum of the sizes for the omitted calls as specified
above, plus the size for the extended call had it not been extended, plus any
padding necessary to align the allocated objects within the allocated memory.
\pnum
\indextext{placement new-expression@placement \gterm{new-expression}|see{\gterm{new-expression}, placement}}%
The \grammarterm{new-placement} syntax is used to supply additional
arguments to an allocation function; such an expression is called
a \defnx{placement \grammarterm{new-expression}}{\idxgram{new-expression}!placement}.
\pnum
Overload resolution is
performed on a function call created by assembling an argument list.
The first argument is
the amount of space requested,
and has type \tcode{std::size_t}.
If the type of the allocated object has new-extended alignment,
the next argument is
the type's alignment,
and has type \tcode{std::align_val_t}.
If the \grammarterm{new-placement} syntax is used,
the \grammarterm{initializer-clause}{s}
in its \grammarterm{expression-list}
are the succeeding arguments.
If no matching function is found then
\begin{itemize}
\item
if the allocated object type has new-extended alignment,
the alignment argument is removed from the argument list;
\item
otherwise, an argument that
is the type's alignment and has type \tcode{std::align_val_t}
is added into the argument list immediately after the first argument;
\end{itemize}
and then overload resolution is performed again.
\pnum
\begin{example}
\begin{itemize}
\item \tcode{new T} results in one of the following calls:
\begin{codeblock}
operator new(sizeof(T))
operator new(sizeof(T), std::align_val_t(alignof(T)))
\end{codeblock}
\item \tcode{new(2,f) T} results in one of the following calls:
\begin{codeblock}
operator new(sizeof(T), 2, f)
operator new(sizeof(T), std::align_val_t(alignof(T)), 2, f)
\end{codeblock}
\item \tcode{new T[5]} results in one of the following calls:
\begin{codeblock}
operator new[](sizeof(T) * 5 + x)
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)))
\end{codeblock}
\item \tcode{new(2,f) T[5]} results in one of the following calls:
\begin{codeblock}
operator new[](sizeof(T) * 5 + x, 2, f)
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)), 2, f)
\end{codeblock}
\end{itemize}
Here, each instance of \tcode{x} is a non-negative unspecified value
representing array allocation overhead; the result of the
\grammarterm{new-expression} will be offset by this amount from the value
returned by \tcode{operator new[]}. This overhead may be applied in all
array \grammarterm{new-expression}{s}, including those referencing
a placement allocation function, except when referencing
the library function \tcode{operator new[](std::size_t, void*)}.
The amount of overhead may vary from one
invocation of \keyword{new} to another.
\end{example}
\pnum
\begin{note}
Unless an allocation function has a non-throwing
exception specification\iref{except.spec},
it indicates failure to allocate storage by throwing a
\indextext{\idxcode{bad_alloc}}%
\indexlibraryglobal{bad_alloc}%
\tcode{std::bad_alloc}
exception\iref{basic.stc.dynamic.allocation,except,bad.alloc};
it returns a non-null pointer otherwise. If the allocation function
has a non-throwing exception specification,
it returns null to indicate failure to allocate storage
and a non-null pointer otherwise.
\end{note}
If the allocation function is a non-allocating
form\iref{new.delete.placement} that returns null,
the behavior is undefined.
Otherwise,
if the allocation function returns null, initialization shall not be
done, the deallocation function shall not be called, and the value of
the \grammarterm{new-expression} shall be null.
\pnum
\begin{note}
When the allocation function returns a value other than null, it must be
a pointer to a block of storage in which space for the object has been
reserved. The block of storage is assumed to be
appropriately aligned\iref{basic.align}
and of the requested size. The address of the created object will not
necessarily be the same as that of the block if the object is an array.
\end{note}
\pnum
\indextext{\idxcode{new}!array of class objects and}%
\indextext{\idxcode{new}!initialization and}%
\indextext{\idxcode{new}!constructor and}%
\indextext{\idxcode{new}!default constructor and}%
A \grammarterm{new-expression} that creates an object of type \tcode{T}
initializes that object as follows:
\begin{itemize}
\item If the \grammarterm{new-initializer} is omitted, the object is
default-initialized\iref{dcl.init}.
\begin{note}
If no initialization
is performed, the object has an indeterminate value.
\end{note}
\item Otherwise, the \grammarterm{new-initializer} is interpreted according to
the initialization rules of~\ref{dcl.init} for direct-initialization.
\end{itemize}
\pnum
\indextext{\idxcode{new}!unspecified order of evaluation}%
\indextext{\idxcode{new}!unspecified constructor and}%
The invocation of the allocation function is sequenced before
the evaluations of expressions in the \grammarterm{new-initializer}. Initialization of
the allocated object is sequenced before the
\indextext{value computation}%
value computation of the
\grammarterm{new-expression}.
\pnum
If the \grammarterm{new-expression}
creates an array of objects of class type, the destructor is potentially
invoked\iref{class.dtor}.
\pnum
\indextext{\idxcode{new}!exception and}%
If any part of the object initialization described above%
\begin{footnote}
This can
include evaluating a \grammarterm{new-initializer} and/or calling
a constructor.
\end{footnote}
terminates by throwing an exception and a suitable deallocation function
can be found, the deallocation function is called to free the memory in
which the object was being constructed, after which the exception
continues to propagate in the context of the \grammarterm{new-expression}.
If no unambiguous matching deallocation function can be found,
propagating the exception does not cause the object's memory to be
freed.
\begin{note}
This is appropriate when the called allocation function does not
allocate memory; otherwise, it is likely to result in a memory leak.
\end{note}
\pnum
If the \grammarterm{new-expression} does not begin with
a unary \tcode{::} operator and
the allocated type is a class type \tcode{T} or an array thereof,
a search is performed for the deallocation function's name
in the scope of \tcode{T}.
Otherwise, or if nothing is found,
the deallocation function's name is looked up by
searching for it in the global scope.
\pnum
A declaration of a placement deallocation function matches the
declaration of a placement allocation function if it has the same number
of parameters and, after parameter transformations\iref{dcl.fct}, all
parameter types except the first are identical. If
the lookup finds a single matching deallocation function, that function
will be called; otherwise, no deallocation function will be called. If
the lookup finds a usual deallocation
function
and that function,
considered as a placement deallocation function, would have been
selected as a match for the allocation function, the program is
ill-formed. For a non-placement allocation function, the normal deallocation
function lookup is used to find the matching deallocation
function\iref{expr.delete}.
In any case,
the matching deallocation function (if any) shall be non-deleted and
accessible from the point where the \grammarterm{new-expression} appears.
\begin{example}
\begin{codeblock}
struct S {
// Placement allocation function:
static void* operator new(std::size_t, std::size_t);
// Usual (non-placement) deallocation function:
static void operator delete(void*, std::size_t);
};
S* p = new (0) S; // error: non-placement deallocation function matches
// placement allocation function
\end{codeblock}
\end{example}
\pnum
If a \grammarterm{new-expression} calls a deallocation function, it passes
the value returned from the allocation function call as the first
argument of type \tcode{\keyword{void}*}. If a placement deallocation function is
called, it is passed the same additional arguments as were passed to the
placement allocation function, that is, the same arguments as those
specified with the \grammarterm{new-placement} syntax.
If the implementation is allowed
to introduce a temporary object or make a copy of any argument
as part of the call to the allocation function,
it is unspecified whether the same object is used in the call
to both the allocation and deallocation functions.
\rSec3[expr.delete]{Delete}
\pnum
\indextext{expression!\idxcode{delete}}%
\indextext{\idxcode{delete}}%
The \grammarterm{delete-expression} operator destroys a most derived
object\iref{intro.object} or array created by a
\grammarterm{new-expression}.
\begin{bnf}
\nontermdef{delete-expression}\br
\opt{\terminal{::}} \keyword{delete} cast-expression\br
\opt{\terminal{::}} \keyword{delete} \terminal{[} \terminal{]} cast-expression
\end{bnf}
The first alternative is a
\defnx{single-object delete expression}{delete!single-object}, and the
second is an \defnx{array delete expression}{delete!array}.
Whenever the \keyword{delete} keyword is immediately followed by empty square
brackets, it shall be interpreted as the second alternative.
\begin{footnote}
A
\grammarterm{lambda-expression} with a \grammarterm{lambda-introducer}
that consists of empty square brackets can follow the \keyword{delete} keyword
if the \grammarterm{lambda-expression} is enclosed in parentheses.
\end{footnote}
If the operand is of
class type, it is contextually implicitly converted\iref{conv}
to a pointer to object
type
and the converted operand is used in place of the original operand
for the remainder of this subclause.
Otherwise, it shall be a prvalue of pointer to object type.
The \grammarterm{delete-expression} has type
\keyword{void}.
\pnum
\indextext{\idxcode{delete}!single-object}%
In a single-object delete expression, the value of the operand of
\keyword{delete} may be a null pointer value,
a pointer value
that resulted from a previous non-array \grammarterm{new-expression}, or
a pointer to a base class subobject
of an object created by such a \grammarterm{new-expression}.
If not, the behavior is undefined.
\indextext{array!\idxcode{delete}}%
In an array delete expression, the value of the operand of \keyword{delete}
may be a null pointer value or a pointer value that resulted from
a previous array \grammarterm{new-expression} whose
allocation function was not a non-allocating form\iref{new.delete.placement}.
\begin{footnote}
For nonzero-length
arrays, this is the same as a pointer to the first
element of the array created by that \grammarterm{new-expression}.
Zero-length arrays do not have a first element.
\end{footnote}
If not, the behavior is undefined.
\begin{note}
This means that the syntax of the \grammarterm{delete-expression} must
match the type of the object allocated by \keyword{new}, not the syntax of the
\grammarterm{new-expression}.
\end{note}
\begin{note}
A pointer to a \keyword{const} type can be the operand of a
\grammarterm{delete-expression}; it is not necessary to cast away the
constness\iref{expr.const.cast} of the pointer expression before it is
used as the operand of the \grammarterm{delete-expression}.
\end{note}
\pnum
\indextext{\idxcode{delete}!undefined}%
In a single-object delete expression, if the static type of the object to be
deleted is not similar\iref{conv.qual} to its dynamic type
and the selected deallocation function (see below)
is not a destroying operator delete,
the static type shall be a base
class of the dynamic type of the object to be deleted and the static type shall
have a virtual destructor or the behavior is undefined. In an array delete
expression, if the dynamic type of the object to be deleted is not similar to
its static type, the behavior is undefined.
\pnum
\indextext{type!incomplete}%
If the object being deleted has incomplete class type at the point of deletion,
the program is ill-formed.
\pnum
\indextext{\idxcode{delete}!destructor and}%
If the value of the operand of the \grammarterm{delete-expression} is not a
null pointer value
and the selected deallocation function (see below)
is not a destroying operator delete,
evaluating the \grammarterm{delete-expression} invokes the
destructor (if any) for the object or the elements of the array being
deleted.
The destructor shall be accessible from the point where
the \grammarterm{delete-expression} appears.
In the case of an array, the elements are destroyed in
order of decreasing address (that is, in reverse order of the completion
of their constructor; see~\ref{class.base.init}).
\pnum
If the value of the operand of the \grammarterm{delete-expression} is not a
null pointer value, then:
\begin{itemize}
\item
If the allocation call for the \grammarterm{new-expression} for the object to
be deleted was not omitted and the allocation was not extended\iref{expr.new}, the
\grammarterm{delete-expression} shall call a deallocation
function\iref{basic.stc.dynamic.deallocation}. The value returned from the
allocation call of the \grammarterm{new-expression} shall be passed as the
first argument to the deallocation function.
\item
Otherwise, if the allocation was extended or was provided by extending the
allocation of another \grammarterm{new-expression}, and the
\grammarterm{delete-expression} for every other pointer value produced by a
\grammarterm{new-expression} that had storage provided by the extended
\grammarterm{new-expression} has been evaluated, the
\grammarterm{delete-expression} shall call a deallocation function. The value
returned from the allocation call of the extended \grammarterm{new-expression}
shall be passed as the first argument to the deallocation function.
\item
Otherwise, the \grammarterm{delete-expression} will not call a
deallocation function.
\end{itemize}
\begin{note}
The deallocation function is called regardless of whether the destructor
for the object or some element of the array throws an exception.
\end{note}
If the value of the operand of the \grammarterm{delete-expression} is a
null pointer value, it is unspecified whether a deallocation function will be
called as described above.
\pnum
If a deallocation function is called,
it is \tcode{\keyword{operator} \keyword{delete}} for a single-object delete expression or
\tcode{\keyword{operator} \keyword{delete}[]} for an array delete expression.
\begin{note}
\indextext{\idxcode{operator delete}}%
An implementation provides default definitions of the global
deallocation functions\iref{new.delete.single,new.delete.array}.
A \Cpp{} program can provide alternative definitions of these
functions\iref{replacement.functions}, and/or class-specific
versions\iref{class.free}.
\end{note}
\pnum
If the keyword \keyword{delete} in a \grammarterm{delete-expression}
is not preceded by the unary \tcode{::} operator and the type of the operand is
a pointer to a (possibly cv-qualified) class type \tcode{T}
or (possibly multidimensional) array thereof:
\begin{itemize}
\item
For a single-object delete expression,
if the operand is a pointer to \cv{} \tcode{T} and
\tcode{T} has a virtual destructor,
the deallocation function is the one selected at the point of definition of
the dynamic type's virtual destructor\iref{class.dtor}.
\item
Otherwise,
a search is performed for the deallocation function's name
in the scope of \tcode{T}.
\end{itemize}
Otherwise, or if nothing is found,
the deallocation function's name is looked up by
searching for it in the global scope.
In any case, any declarations
other than of usual deallocation functions\iref{basic.stc.dynamic.deallocation}
are discarded.
\begin{note}
If only a placement deallocation function is found in a class,
the program is ill-formed because the lookup set is empty\iref{basic.lookup}.
\end{note}
\pnum
The deallocation function to be called is selected as follows:
\begin{itemize}
\item
If any of the deallocation functions is a destroying operator delete,
all deallocation functions that are not destroying operator deletes
are eliminated from further consideration.
\item
If the type has new-extended alignment,
a function with a parameter of type \tcode{std::align_val_t} is preferred;
otherwise a function without such a parameter is preferred.
If any preferred functions are found,
all non-preferred functions are eliminated from further consideration.
\item
If exactly one function remains,
that function is selected and the selection process terminates.
\item
If the deallocation functions belong to a class scope,
the one without a parameter of type \tcode{std::size_t} is selected.
\item
If the type is complete
and if, for an array delete expression only,
the operand is a pointer to a class type with a
non-trivial destructor or a (possibly multidimensional) array thereof,
the function with a parameter of type \tcode{std::size_t} is selected.
\item
Otherwise, it is unspecified
whether a deallocation function with a parameter of type \tcode{std::size_t}
is selected.
\end{itemize}
Unless the deallocation function is selected
at the point of definition of the dynamic type's virtual destructor,
the selected deallocation function shall be accessible
from the point where the \grammarterm{delete-expression} appears.
\pnum
For a single-object delete expression,
the deleted object is
the object $A$ pointed to by the operand
if the static type of $A$ does not have a virtual destructor,
and the most-derived object of $A$ otherwise.
\begin{note}
If the deallocation function is not a destroying operator delete
and the deleted object is not the most derived object in the former case,
the behavior is undefined,
as stated above.
\end{note}
For an array delete expression,
the deleted object is
the array object.
When a \grammarterm{delete-expression}
is executed, the selected deallocation function shall be called with
the address of the deleted object
in a single-object delete expression, or
the address of the deleted object
suitably adjusted for the array allocation
overhead\iref{expr.new} in an array delete expression,
as its first argument.
\begin{note}
Any cv-qualifiers in the type of the deleted object
are ignored when forming this argument.
\end{note}
If a destroying operator delete is used,
an unspecified value
is passed as the argument
corresponding to the parameter of type \tcode{std::destroying_delete_t}.
If a deallocation function
with a parameter of type \tcode{std::align_val_t}
is used,
the alignment of the type of the deleted object
is passed as the corresponding argument.
If a deallocation function
with a parameter of type \tcode{std::size_t} is used,
the size of the deleted object
in a single-object delete expression, or
of the array plus allocation overhead
in an array delete expression,
is passed as the corresponding argument.
\begin{note}
If this results in a call to a replaceable deallocation function,
and either
the first argument was not the result of
a prior call to a replaceable allocation function or
the second or third argument was not the corresponding argument in said call,
the behavior is undefined\iref{new.delete.single,new.delete.array}.
\end{note}
\rSec3[expr.reflect]{The reflection operator}
\begin{bnf}
\nontermdef{reflect-expression}\br
\terminal{\caret\caret} \terminal{::}\br
\terminal{\caret\caret} reflection-name\br
\terminal{\caret\caret} type-id\br
\terminal{\caret\caret} id-expression
\end{bnf}
\begin{bnf}
\nontermdef{reflection-name}\br
\opt{nested-name-specifier} identifier\br
nested-name-specifier \keyword{template} identifier
\end{bnf}
\pnum
The unary \tcode{\caret\caret} operator,
called the \defnadj{reflection}{operator},
yields a prvalue of type \tcode{std::meta::info}\iref{basic.fundamental}.
\begin{note}
This document places no restriction on representing, by reflections,
constructs not described by this document or
using the names of such constructs
as operands of \grammarterm{reflect-expression}s.
\end{note}
\pnum
The component names of a \grammarterm{reflection-name}
are those of its \grammarterm{nested-name-specifier} (if any) and
its \grammarterm{identifier}.
The terminal name of a \grammarterm{reflection-name} of the form
\grammarterm{nested-name-specifier} \keyword{template} \grammarterm{identifier}
shall denote a template.
\pnum
A \grammarterm{reflect-expression} is parsed as
the longest possible sequence of tokens
that could syntactically form a \grammarterm{reflect-expression}.
An unparenthesized \grammarterm{reflect-expression}
that represents a template shall not be followed by \tcode{<}.
\begin{example}
\begin{codeblock}
static_assert(std::meta::is_type(^^int())); // \tcode{\caret\caret} applies to the type-id \tcode{int()}
template struct X {};
consteval bool operator<(std::meta::info, X) { return false; }
consteval void g(std::meta::info r, X xv) {
r == ^^int && true; // error: \tcode{\caret\caret} applies to the \grammarterm{type-id} \tcode{int\&\&}
r == ^^int & true; // error: \tcode{\caret\caret} applies to the type-id \tcode{int\&}
r == (^^int) && true; // OK
r == ^^int &&&& true; // error: \tcode{int \&\&\&\&} is not a valid \grammarterm{type-id}
^^X < xv; // error: \grammarterm{reflect-expression} that represents a template is followed by \tcode{<}
(^^X) < xv; // OK
^^X < xv; // OK
}
\end{codeblock}
\end{example}
\pnum
A \grammarterm{reflect-expression} of the form \tcode{\caret\caret ::}
represents the global namespace.
\pnum
If a \grammarterm{reflect-expression} $R$ matches
the form \tcode{\caret\caret \grammarterm{reflection-name}},
it is interpreted as such;
the \grammarterm{identifier} is looked up and
the representation of $R$ is determined as follows:
\begin{itemize}
\item
If lookup finds a declaration
that replaced a \grammarterm{using-declarator}
during a single search\iref{basic.lookup.general,namespace.udecl},
$R$ is ill-formed.
\begin{example}
\begin{codeblock}
struct A { struct S {}; };
struct B : A { using A::S; };
constexpr std::meta::info r1 = ^^B::S; // error: \tcode{A::S} found through \grammarterm{using-declarator}
struct C : virtual B { struct S {}; };
struct D : virtual B, C {};
D::S s; // OK, names \tcode{C::S} per \ref{class.member.lookup}
constexpr std::meta::info r2 = ^^D::S; // OK, result \tcode{C::S} not found through \grammarterm{using-declarator}
\end{codeblock}
\end{example}
\item
Otherwise, if lookup finds a namespace alias\iref{namespace.alias},
$R$ represents that namespace alias.
For any other \grammarterm{namespace-name},
$R$ represents the denoted namespace.
\item
Otherwise, if lookup finds a namespace\iref{basic.namespace},
$R$ represents that namespace.
\item
Otherwise, if lookup finds a concept\iref{temp.concept},
$R$ represents the denoted concept.
\item
Otherwise, if lookup finds a template\iref{temp.names},
the representation of $R$ is determined as follows:
\begin{itemize}
\item
If lookup finds an injected-class-name\iref{class.pre}, then:
\begin{itemize}
\item
If the \grammarterm{reflection-name} is of the form
\tcode{\grammarterm{nested-name-specifier} \keyword{template} \grammarterm{identifier}},
then $R$ represents the class template named by the injected-class-name.
\item
Otherwise, the injected-class-name shall be unambiguous
when considered as a \grammarterm{type-name} and
$R$ represents the class template specialization so named.
\end{itemize}
\item
Otherwise, if lookup finds an overload set,
that overload set shall contain only
declarations of a unique function template F;
$R$ represents F.
\item
Otherwise, if lookup finds
a class template, variable template, or alias template,
$R$ represents that template.
\begin{note}
Lookup never finds a partial or explicit specialization.
\end{note}
\end{itemize}
\item
Otherwise, if lookup finds a type alias $A$,
$R$ represents the underlying entity of $A$
if $A$ was introduced by the declaration of a template parameter;
otherwise, $R$ represents $A$.
\item
Otherwise, if lookup finds a class or an enumeration,
$R$ represents the denoted type.
\item
Otherwise, if lookup finds a class member of an anonymous union\iref{class.union.anon}, $R$ represents that class member.
\item
Otherwise,
the \grammarterm{reflection-name} shall be an \grammarterm{id-expression} \tcode{I}
and $R$ is \tcode{\caret\caret I} (see below).
\end{itemize}
\pnum
A \grammarterm{reflect-expression} $R$ of the form
\tcode{\caret\caret \grammarterm{type-id}}
represents an entity determined as follows:
\begin{itemize}
\item
If the \grammarterm{type-id} designates
a placeholder type\iref{dcl.spec.auto.general},
$R$ is ill-formed.
\item
Otherwise, if the \grammarterm{type-id} names a type alias
that is a specialization of an alias template\iref{temp.alias},
$R$ represents that type alias.
\item
Otherwise, $R$ represents the type denoted by the \grammarterm{type-id}.
\end{itemize}
\pnum
A \grammarterm{reflect-expression} $R$ of the form
\tcode{\caret\caret \grammarterm{id-expression}}
represents an entity determined as follows:
\begin{itemize}
\item
If the \grammarterm{id-expression} denotes
\begin{itemize}
\item
a variable declared by
an \grammarterm{init-capture}\iref{expr.prim.lambda.capture},
\item
a function-local predefined variable\iref{dcl.fct.def.general},
\item
a local parameter introduced by
a \grammarterm{requires-expression}\iref{expr.prim.req}, or
\item
a local entity $E$\iref{basic.pre} for which a lambda scope intervenes
between the point at which $E$ was introduced and $R$,
\end{itemize}
then $R$ is ill-formed.
\item
Otherwise, if the \grammarterm{id-expression} denotes an overload set $S$,
overload resolution for the expression \tcode{\&S} with no target
shall select a unique function\iref{over.over};
$R$ represents that function.
\item
Otherwise, if the \grammarterm{id-expression} denotes
a variable, structured binding, enumerator, or non-static data member,
$R$ represents that entity.
\item
Otherwise, $R$ is ill-formed.
\begin{note}
This includes \grammarterm{unqualified-id}s
that name a constant template parameter and
\grammarterm{pack-index-expression}s.
\end{note}
\end{itemize}
The \grammarterm{id-expression} of
a \grammarterm{reflect-expression} is an unevaluated operand\iref{expr.context}.
\begin{example}
\begin{codeblock}
template void fn() requires (^^T != ^^int);
template void fn() requires (^^T == ^^int);
template void fn() requires (sizeof(T) == sizeof(int));
constexpr std::meta::info a = ^^fn; // OK
constexpr std::meta::info b = ^^fn; // error: ambiguous
constexpr std::meta::info c = ^^std::vector; // OK
template
struct S {
static constexpr std::meta::info r = ^^T;
using type = T;
};
static_assert(S::r == ^^int);
static_assert(^^S::type != ^^int);
typedef struct X {} Y;
typedef struct Z {} Z;
constexpr std::meta::info e = ^^Y; // OK, represents the type alias \tcode{Y}
constexpr std::meta::info f = ^^Z; // OK, represents the type alias \tcode{Z}, not the type\iref{basic.lookup.general}
\end{codeblock}
\end{example}
\rSec2[expr.cast]{Explicit type conversion (cast notation)}%
\indextext{expression!cast|(}
\pnum
The result of the expression \tcode{(T)} \grammarterm{cast-expression} is
of type \tcode{T}. The result is an lvalue if \tcode{T} is an lvalue
reference type or an rvalue reference to function type and an xvalue if \tcode{T}
is an rvalue reference to object type; otherwise the result is a prvalue.
\begin{note}
If \tcode{T} is a non-class type that is cv-qualified, the
\grammarterm{cv-qualifier}{s} are discarded when determining the type of the
resulting prvalue; see \ref{expr.prop}.
\end{note}
\pnum
An explicit type conversion can be expressed using functional
notation\iref{expr.type.conv}, a type conversion operator
(\keyword{dynamic_cast}, \keyword{static_cast}, \keyword{reinterpret_cast},
\keyword{const_cast}), or the \term{cast} notation.
\begin{bnf}
\nontermdef{cast-expression}\br
unary-expression\br
\terminal{(} type-id \terminal{)} cast-expression
\end{bnf}
\pnum
Any type conversion not mentioned below and not explicitly defined by
the user\iref{class.conv} is ill-formed.
\pnum
The conversions performed by
\begin{itemize}
\indextext{cast!const}%
\indextext{cast!static}%
\indextext{cast!reinterpret}%
\item a \keyword{const_cast}\iref{expr.const.cast},
\item a \keyword{static_cast}\iref{expr.static.cast},
\item a \keyword{static_cast} followed by a \keyword{const_cast},
\item a \keyword{reinterpret_cast}\iref{expr.reinterpret.cast}, or
\item a \keyword{reinterpret_cast} followed by a \keyword{const_cast},
\end{itemize}
can be performed using the cast notation of explicit type conversion.
The same semantic restrictions and behaviors apply, with the exception
that in performing a \keyword{static_cast} in the following situations the
conversion is valid even if the base class is inaccessible:
\begin{itemize}
\item a pointer to an object of derived class type or an lvalue or
rvalue of derived class type may be explicitly converted to a pointer or
reference to an unambiguous base class type, respectively;
\item a pointer to member of derived class type may be explicitly
converted to a pointer to member of an unambiguous non-virtual base
class type;
\item a pointer to an object of an unambiguous non-virtual base class
type, a glvalue of an unambiguous non-virtual base class type,
or a pointer to member of an unambiguous non-virtual base class type may
be explicitly converted to a pointer, a reference, or a pointer to
member of a derived class type, respectively.
\end{itemize}
If a conversion can be interpreted in more than one of the ways listed
above, the interpretation that appears first in the list is used, even
if a cast resulting from that interpretation is ill-formed. If a
\keyword{static_cast} followed by a \keyword{const_cast} is used and
the conversion can be interpreted in more than one way as such,
the conversion is
ill-formed.
\begin{example}
\begin{codeblock}
struct A { };
struct I1 : A { };
struct I2 : A { };
struct D : I1, I2 { };
A* foo( D* p ) {
return (A*)( p ); // ill-formed \keyword{static_cast} interpretation
}
int*** ptr = 0;
auto t = (int const*const*const*)ptr; // OK, \keyword{const_cast} interpretation
struct S {
operator const int*();
operator volatile int*();
};
int *p = (int*)S(); // error: two possible interpretations using \keyword{static_cast} followed by \keyword{const_cast}
\end{codeblock}
\end{example}
\pnum
\indextext{class!cast to incomplete}%
The operand of a cast using the cast notation can be a prvalue of type
``pointer to incomplete class type''. The destination type of a cast
using the cast notation can be ``pointer to incomplete class type''. If
both the operand and destination types are class types and one or both
are incomplete, it is unspecified whether the \keyword{static_cast} or the
\keyword{reinterpret_cast} interpretation is used, even if there is an
inheritance relationship between the two classes.
\begin{note}
For example, if the classes were defined later in the translation unit,
a multi-pass compiler could validly interpret a cast between
pointers to the classes as if the class types were complete at the point
of the cast.
\end{note}
\indextext{expression!cast|)}
\rSec2[expr.mptr.oper]{Pointer-to-member operators}
\pnum
\indextext{expression!pointer-to-member}%
\indextext{pointer to member}%
\indextext{operator!pointer to member}%
\indextext{\idxcode{.*}|see{operator, pointer to member}}%
\indextext{operator!pointer to member}%
\indextext{\idxcode{->*}|see{operator, pointer to member}}%
The pointer-to-member operators \tcode{->*} and \tcode{.*} group
left-to-right.
\begin{bnf}
\nontermdef{pm-expression}\br
cast-expression\br
pm-expression \terminal{.*} cast-expression\br
pm-expression \terminal{->*} cast-expression
\end{bnf}
\pnum
The binary operator \tcode{.*} binds its second operand, which shall be
a prvalue
of type ``pointer to member of \tcode{T}'' to its first operand, which shall be
a glvalue
of
class \tcode{T} or of a class of which \tcode{T} is an unambiguous and
accessible base class. The result is an object or a function of the type
specified by the second operand.
\pnum
The binary operator \tcode{->*} binds its second operand, which shall be
a prvalue
of type ``pointer to member of \tcode{T}'' to its first operand, which shall be of
type ``pointer to \tcode{U}''
where \tcode{U} is either \tcode{T} or
a class of which \tcode{T}
is an unambiguous and accessible base class.
The expression \tcode{E1->*E2} is converted into the equivalent form
\tcode{(*(E1)).*E2}.
\pnum
Abbreviating \grammarterm{pm-expression}\tcode{.*}\grammarterm{cast-expression} as \tcode{E1.*E2}, \tcode{E1}
is called the \defn{object expression}.
If the result of \tcode{E1} is an object
whose type is not similar to the type of \tcode{E1}, or
whose most derived object does not
contain the member to which
\tcode{E2} refers, the behavior is undefined.
The expression \tcode{E1} is sequenced before the expression \tcode{E2}.
\pnum
The restrictions on cv-qualification, and the manner in which
the cv-qualifiers of the operands are combined to produce the
cv-qualifiers of the result, are the same as the rules for
\tcode{E1.E2} given in~\ref{expr.ref}.
\begin{note}
It is not possible to use a pointer to member that refers to a
\keyword{mutable} member to modify a const class object. For
example,
\begin{codeblock}
struct S {
S() : i(0) { }
mutable int i;
};
void f()
{
const S cs;
int S::* pm = &S::i; // \tcode{pm} refers to \keyword{mutable} member \tcode{S::i}
cs.*pm = 88; // error: \tcode{cs} is a const object
}
\end{codeblock}
\end{note}
\pnum
\indextext{function!pointer to member}%
If the result of \tcode{.*} or \tcode{->*} is a function, then that
result can be used only as the operand for the function call operator
\tcode{()}.
\begin{example}
\begin{codeblock}
(ptr_to_obj->*ptr_to_mfct)(10);
\end{codeblock}
calls the member function denoted by \tcode{ptr_to_mfct} for the object
pointed to by \tcode{ptr_to_obj}.
\end{example}
In a \tcode{.*} expression whose object expression is an rvalue, the program is
ill-formed if the second operand is a pointer to member function
whose \grammarterm{ref-qualifier} is \tcode{\&},
unless its \grammarterm{cv-qualifier-seq} is \keyword{const}.
In a \tcode{.*}
expression whose object expression is an lvalue, the program is ill-formed if the second
operand is
a pointer to member function
whose \grammarterm{ref-qualifier} is \tcode{\&\&}.
The result of a \tcode{.*} expression
whose second operand is a pointer to a data member is an lvalue if the first
operand is an lvalue and an xvalue otherwise. The result of a \tcode{.*} expression whose
second operand is a pointer to a member function is a prvalue.
If the second operand is the null
member pointer value\iref{conv.mem}, the behavior is undefined.
\rSec2[expr.mul]{Multiplicative operators}%
\indextext{expression!multiplicative operators}%
\indextext{operator!multiplicative}
\pnum
The multiplicative operators \tcode{*}, \tcode{/}, and \tcode{\%} group
left-to-right.
\indextext{operator!multiplication}%
\indextext{\idxcode{*}|see{operator, multiplication}}%
\indextext{operator!division}%
\indextext{\idxcode{/}|see{operator, division}}%
\indextext{operator!remainder}%
\indextext{\idxcode{\%}|see{operator, remainder}}%
\indextext{remainder operator|see{operator, remainder}}%
%
\begin{bnf}
\nontermdef{multiplicative-expression}\br
pm-expression\br
multiplicative-expression \terminal{*} pm-expression\br
multiplicative-expression \terminal{/} pm-expression\br
multiplicative-expression \terminal{\%} pm-expression
\end{bnf}
\pnum
The operands of \tcode{*} and \tcode{/} shall have arithmetic or unscoped
enumeration type; the operands of \tcode{\%} shall have integral or unscoped
enumeration type. The usual arithmetic conversions\iref{expr.arith.conv} are performed on the
operands and determine the type of the result.
\pnum
The binary \tcode{*} operator indicates multiplication.
\pnum
The binary \tcode{/} operator yields the quotient, and the binary
\tcode{\%} operator yields the remainder from the division of the first
expression by the second.
\indextext{zero!undefined division by}%
If the second operand of \tcode{/} or \tcode{\%} is zero, the behavior is
undefined.
For integral operands, the \tcode{/} operator yields the algebraic quotient with
any fractional part discarded;
\begin{footnote}
This is often called truncation towards zero.
\end{footnote}
if the quotient \tcode{a/b} is representable in the type of the result,
\tcode{(a/b)*b + a\%b} is equal to \tcode{a}; otherwise, the behavior
of both \tcode{a/b} and \tcode{a\%b} is undefined.
\rSec2[expr.add]{Additive operators}%
\indextext{expression!additive operators}%
\indextext{operator!additive}
\pnum
The additive operators \tcode{+} and \tcode{-} group left-to-right.
Each operand shall be a prvalue.
If both operands have arithmetic or unscoped enumeration type,
the usual arithmetic conversions\iref{expr.arith.conv} are performed.
Otherwise, if one operand has arithmetic or unscoped enumeration type,
integral promotion is applied\iref{conv.prom} to that operand.
A converted or promoted operand is used in place of
the corresponding original operand for the remainder of this section.
\indextext{operator!addition}%
\indextext{addition operator|see{operator, addition}}%
\indextext{\idxcode{+}|see{operator, addition}}%
\indextext{operator!subtraction}%
\indextext{subtraction operator|see{operator, subtraction}}%
\indextext{\idxcode{-}|see{operator, subtraction}}%
%
\begin{bnf}
\nontermdef{additive-expression}\br
multiplicative-expression\br
additive-expression \terminal{+} multiplicative-expression\br
additive-expression \terminal{-} multiplicative-expression
\end{bnf}
\indextext{incomplete}%
For addition, either both operands shall have arithmetic
type, or one operand shall be a pointer to a completely-defined object
type and the other shall have integral type.
\pnum
For subtraction, one of the following shall hold:
\begin{itemize}
\item both operands have arithmetic type; or
\item
\indextext{arithmetic!pointer}%
both operands are pointers to cv-qualified or cv-unqualified
versions of the same completely-defined object type; or
\item the left operand is a pointer to a completely-defined object type
and the right operand has integral type.
\end{itemize}
\pnum
The result of the binary \tcode{+} operator is the sum of the operands.
The result of the binary \tcode{-} operator is the difference resulting
from the subtraction of the second operand from the first.
\pnum
\indextext{arithmetic!pointer}%
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 a (possibly-hypothetical) array element $i$
of an array object \tcode{x} with $n$ elements\iref{dcl.array},
\begin{footnote}
As specified in \ref{basic.compound},
an object that is not an array element
is considered to belong to a single-element array for this purpose and
a pointer past the last element of an array of $n$ elements
is considered to be equivalent to a pointer to a hypothetical array element
$n$ for this purpose.
\end{footnote}
the expressions \tcode{P + J} and \tcode{J + P}
(where \tcode{J} has the value $j$)
point to the (possibly-hypothetical) array element
$i + j$ of \tcode{x} if $0 \le i + j \le n$
and the expression \tcode{P - J}
points to the (possibly-hypothetical) array element
$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}%
\indextext{subtraction!implementation-defined pointer}%
\indextext{\idxcode{ptrdiff_t}}%
\indextext{comparison!undefined pointer}%
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 named by
\tcode{std::ptrdiff_t} in the \libheader{cstddef}
header\iref{support.types.layout}.
\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,
array elements $i$ and $j$
of the same array object \tcode{x},
the expression \tcode{P - Q} has the value $i - j$.
\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\iref{expr.pre}.
\end{note}
\item Otherwise, the behavior is undefined.
\end{itemize}
\pnum
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{example}
\begin{codeblock}
int arr[5] = {1, 2, 3, 4, 5};
unsigned int *p = reinterpret_cast(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}
\pnum
\indextext{expression!left-shift-operator}%
\indextext{expression!right-shift-operator}%
\indextext{shift operator!left|see{operator, left shift}}%
\indextext{shift operator!right|see{operator, right shift}}%
\indextext{right shift operator|see{operator, right shift}}%
\indextext{left shift operator|see{operator, left shift}}%
The shift operators \tcode{<<} and \tcode{>>} group left-to-right.
\indextext{operator!left shift}%
\indextext{\idxcode{<<}|see{operator, left shift}}%
\indextext{operator!right shift}%
\indextext{\idxcode{>>}|see{operator, right shift}}%
%
\begin{bnf}
\nontermdef{shift-expression}\br
additive-expression\br
shift-expression \terminal{<<} additive-expression\br
shift-expression \terminal{>>} additive-expression
\end{bnf}
The operands shall be prvalues of integral or unscoped enumeration type and integral
promotions are performed. The type of the result is that of the promoted
left operand.
\indextext{left shift!undefined}%
The behavior is undefined if the right operand is negative, or greater
than or equal to the width of the promoted left operand.
\pnum
The value of \tcode{E1 << E2} is the unique value congruent to
$\tcode{E1} \times 2^\tcode{E2}$ modulo $2^N$,
where $N$ is the width of the type of the result.
\begin{note}
\tcode{E1} is left-shifted \tcode{E2} bit positions;
vacated bits are zero-filled.
\end{note}
\pnum
The value of \tcode{E1 >> E2} is $\tcode{E1} / 2^\tcode{E2}$,
rounded towards negative infinity.
\begin{note}
\tcode{E1} is right-shifted \tcode{E2} bit positions.
Right-shift on signed integral types is an arithmetic right shift,
which performs sign-extension.
\end{note}
\pnum
The expression \tcode{E1} is sequenced before the expression \tcode{E2}.
\rSec2[expr.spaceship]{Three-way comparison operator}
\indextext{expression!three-way comparison}%
\indextext{expression!spaceship}%
\pnum
The three-way comparison operator groups left-to-right.
\indextext{\idxcode{<=>}|see{operator, three-way comparison}}%
\indextext{operator!three-way comparison}%
\indextext{operator!spaceship}%
\begin{bnf}
\nontermdef{compare-expression}\br
shift-expression\br
compare-expression \terminal{<=>} shift-expression
\end{bnf}
\pnum
The expression \tcode{p <=> q} is a prvalue indicating whether
\tcode{p} is less than, equal to, greater than, or incomparable with
\tcode{q}.
\pnum
If one of the operands is of type \keyword{bool}
and the other is not, the program is ill-formed.
\pnum
If both operands have arithmetic types,
or one operand has integral type and
the other operand has unscoped enumeration type,
the usual arithmetic conversions\iref{expr.arith.conv} are applied to the operands.
Then:
\begin{itemize}
\item
If a narrowing conversion\iref{dcl.init.list} is required,
other than from an integral type to a floating-point type,
the program is ill-formed.
\item
Otherwise, if the operands have integral type,
the result is of type \tcode{std::strong_ordering}.
The result is
\tcode{std::strong_ordering::equal}
if both operands are arithmetically equal,
\tcode{std::strong_ordering::less}
if the first operand is arithmetically
less than the second operand,
and
\tcode{std::strong_ordering::greater}
otherwise.
\item
Otherwise, the operands have floating-point type, and
the result is of type \tcode{std::partial_ordering}.
The expression \tcode{a <=> b} yields
\tcode{std::partial_ordering::less}
if \tcode{a} is less than \tcode{b},
\tcode{std::partial_ordering::greater}
if \tcode{a} is greater than \tcode{b},
\tcode{std::partial_ordering::equivalent}
if \tcode{a} is equivalent to \tcode{b},
and
\tcode{std::partial_ordering::unordered} otherwise.
\end{itemize}
\pnum
If both operands have the same enumeration type \tcode{E},
the operator yields the result of
converting the operands to the underlying type of \tcode{E}
and applying \tcode{<=>} to the converted operands.
\pnum
If at least one of the operands is of object pointer type and
the other operand is of object pointer or array type,
array-to-pointer conversions\iref{conv.array},
pointer conversions\iref{conv.ptr},
and
qualification conversions\iref{conv.qual}
are performed on both operands
to bring them to their composite pointer type\iref{expr.type}.
After the conversions, the operands shall have the same type.
\begin{note}
If both of the operands are arrays,
array-to-pointer conversions\iref{conv.array} are not applied.
\end{note}
In this case,
\tcode{p <=> q} is of type \tcode{std::strong_ordering} and
the result is defined by the following rules:
\begin{itemize}
\item
If two pointer operands \tcode{p} and \tcode{q} compare equal\iref{expr.eq},
\tcode{p <=> q} yields \tcode{std::strong_ordering::equal};
\item
otherwise, if \tcode{p} and \tcode{q} compare unequal,
\tcode{p <=> q} yields
\tcode{std::strong_ordering::less}
if \tcode{q} compares greater than \tcode{p}
and
\tcode{std::strong_ordering::greater}
if \tcode{p} compares greater than \tcode{q}\iref{expr.rel};
\item
otherwise, the result is unspecified.
\end{itemize}
\pnum
Otherwise, the program is ill-formed.
\pnum
The three comparison category types\iref{cmp.categories}
(the types
\tcode{std::strong_ordering},
\tcode{std::weak_ordering}, and
\tcode{std::partial_ordering})
are not predefined;
if a standard library declaration\iref{compare.syn,std.modules}
of such a class type does not precede\iref{basic.lookup.general}
a use of that type ---
even an implicit use in which the type is not named
(e.g., via the \keyword{auto} specifier\iref{dcl.spec.auto}
in a defaulted three-way comparison\iref{class.spaceship}
or use of the built-in operator) --- the program is ill-formed.
\rSec2[expr.rel]{Relational operators}%
\indextext{expression!relational operators}%
\indextext{operator!relational}
\pnum
The relational operators group left-to-right.
\begin{example}
\tcode{a}|see{operator, greater than}}%
\indextext{operator!less than or equal to}%
\indextext{\idxcode{<=}|see{operator, less than or equal to}}%
\indextext{operator!greater than or equal to}%
\indextext{\idxcode{>=}|see{operator, greater than or equal to}}%
%
\begin{bnf}
\nontermdef{relational-expression}\br
compare-expression\br
relational-expression \terminal{<} compare-expression\br
relational-expression \terminal{>} compare-expression\br
relational-expression \terminal{<=} compare-expression\br
relational-expression \terminal{>=} compare-expression
\end{bnf}
%
The
lvalue-to-rvalue\iref{conv.lval}
and function-to-pointer\iref{conv.func}
standard conversions are performed on the operands.
If one of the operands is a pointer, the
array-to-pointer conversion\iref{conv.array} is performed on the other operand.
\pnum
The converted operands shall have arithmetic, enumeration, or pointer type.
The
operators \tcode{<} (less than), \tcode{>} (greater than), \tcode{<=}
(less than or equal to), and \tcode{>=} (greater than or equal to) all
yield \keyword{false} or \keyword{true}. The type of the result is
\keyword{bool}.
\pnum
The usual arithmetic conversions\iref{expr.arith.conv} are performed on operands of arithmetic
or enumeration type. If both converted operands are pointers,
pointer conversions\iref{conv.ptr},
function pointer conversions\iref{conv.fctptr}, and
qualification conversions\iref{conv.qual}
are performed to bring
them to their composite pointer type\iref{expr.type}.
After conversions, the operands shall have the same type.
\pnum
The result of comparing unequal pointers to objects
\begin{footnote}
As specified in \ref{basic.compound},
an object that is not an array element
is considered to belong to a
single-element array for this purpose and
a pointer past the last element of an array of $n$ elements
is considered to be equivalent to a pointer to a hypothetical array element
$n$ for this purpose.
\end{footnote}
is defined in terms of a partial order consistent with the following rules:
\begin{itemize}
\item If two pointers point to different elements of the same array, or to
subobjects thereof, the pointer to the element with the higher subscript
is required to compare greater.
\item If two pointers point to different non-static data members of the same
object, or to subobjects of such members, recursively,
the pointer to the later declared member is required to compare greater provided
neither member is a subobject of zero size
and their class is not a union.
\item Otherwise, neither pointer is required to compare greater than the other.
\end{itemize}
\pnum
If two operands \tcode{p} and \tcode{q} compare equal\iref{expr.eq},
\tcode{p<=q} and \tcode{p>=q} both yield \keyword{true} and \tcode{pq} both yield \keyword{false}. Otherwise, if a pointer to object \tcode{p}
compares greater than a pointer \tcode{q}, \tcode{p>=q}, \tcode{p>q},
\tcode{q<=p}, and \tcode{q=p}, and \tcode{q>p} all yield \keyword{false}.
Otherwise, the result of each of the operators is unspecified.
\begin{note}
A relational operator applied
to unequal function pointers
yields an unspecified result.
A pointer value of type ``pointer to \cv{}~\keyword{void}''
can point to an object\iref{basic.compound}.
\end{note}
\pnum
If both operands (after conversions) are of arithmetic or enumeration type, each
of the operators shall yield \keyword{true} if the specified relationship is true
and \keyword{false} if it is false.
\rSec2[expr.eq]{Equality operators}%
\indextext{expression!equality operators}%
\indextext{operator!equality}%
\indextext{operator!inequality}
\begin{bnf}
\nontermdef{equality-expression}\br
relational-expression\br
equality-expression \terminal{==} relational-expression\br
equality-expression \terminal{!=} relational-expression
\end{bnf}
\pnum
The \tcode{==} (equal to) and the \tcode{!=} (not equal to) operators
group left-to-right.
The
lvalue-to-rvalue\iref{conv.lval}
and function-to-pointer\iref{conv.func}
standard conversions are performed on the operands.
If one of the operands is a pointer or a null pointer constant\iref{conv.ptr},
the array-to-pointer conversion\iref{conv.array} is performed
on the other operand.
\pnum
The converted operands shall have scalar type. The operators
\tcode{==} and \tcode{!=} both yield \keyword{true} or \keyword{false}, i.e., a
result of type \keyword{bool}. In each case below, the operands shall have the
same type after the specified conversions have been applied.
\pnum
\indextext{comparison!pointer}%
\indextext{comparison!pointer to function}%
If at least one of the converted operands is a pointer,
pointer conversions\iref{conv.ptr},
function pointer conversions\iref{conv.fctptr}, and
qualification conversions\iref{conv.qual}
are performed on both operands to bring them to their composite pointer type\iref{expr.type}.
Comparing pointers is defined as follows:
\begin{itemize}
\item
If one pointer represents the address of a complete object, and another
pointer represents the address one past the last element of a different
complete object,
\begin{footnote}
As specified in \ref{basic.compound},
an object that is not an array element is
considered to belong to a single-element array for this purpose.
\end{footnote}
the result of the comparison is unspecified.
\item
Otherwise, if the pointers are both null, both point to the same
\indextext{address}%
function, or both represent the same address\iref{basic.compound},
they compare equal.
\item
Otherwise, the pointers compare unequal.
\end{itemize}
\pnum
If at least one of the operands is a pointer to member,
pointer-to-member conversions\iref{conv.mem},
function pointer conversions\iref{conv.fctptr}, and
qualification conversions\iref{conv.qual}
are performed on both operands to bring them to
their composite pointer type\iref{expr.type}.
Comparing pointers to members is defined as follows:
\begin{itemize}
\item
If two pointers to members are both the null member pointer value, they compare
equal.
\item
If only one of two pointers to members is the null member pointer value, they
compare unequal.
\item
If either is a pointer to a virtual member function, the result is unspecified.
\item
If one refers to a member of class \tcode{C1} and the other refers to a member
of a different class \tcode{C2}, where neither is a base class of the other,
the result is unspecified.
\begin{example}
\begin{codeblock}
struct A {};
struct B : A { int x; };
struct C : A { int x; };
int A::*bx = (int(A::*))&B::x;
int A::*cx = (int(A::*))&C::x;
bool b1 = (bx == cx); // unspecified
\end{codeblock}
\end{example}
\item
If both refer to (possibly different) members of the same union\iref{class.union},
they compare equal.
\item
Otherwise, two pointers to members compare equal if they would refer to the same member of
the same most derived object\iref{intro.object} or the same subobject if
indirection with a hypothetical object of the associated
class type were performed, otherwise they compare unequal.
\begin{example}
\begin{codeblock}
struct B {
int f();
};
struct L : B { };
struct R : B { };
struct D : L, R { };
int (B::*pb)() = &B::f;
int (L::*pl)() = pb;
int (R::*pr)() = pb;
int (D::*pdl)() = pl;
int (D::*pdr)() = pr;
bool x = (pdl == pdr); // \tcode{false}
bool y = (pb == pl); // \tcode{true}
\end{codeblock}
\end{example}
\end{itemize}
\pnum
Two operands of type \tcode{std::nullptr_t} or one operand of type
\tcode{std::nullptr_t} and the other a null pointer constant compare equal.
\pnum
If both operands are of type \tcode{std::meta::info},
they compare equal if both operands
\begin{itemize}
\item are null reflection values,
\item represent values that are template-argument-equivalent\iref{temp.type},
\item represent the same object,
\item represent the same entity,
\item represent the same annotation\iref{dcl.attr.annotation},
\item represent the same direct base class relationship, or
\item represent equal data member descriptions\iref{class.mem.general},
\end{itemize}
and they compare unequal otherwise.
\pnum
If two operands compare equal, the result is \keyword{true} for
the \tcode{==} operator and \keyword{false} for the \tcode{!=} operator. If two operands
compare unequal, the result is \keyword{false} for the \tcode{==} operator and
\keyword{true} for the \tcode{!=} operator. Otherwise, the result of each of the
operators is unspecified.
\pnum
If both operands are of arithmetic or enumeration type, the usual arithmetic
conversions\iref{expr.arith.conv} are performed on both operands; each of the operators shall yield
\keyword{true} if the specified relationship is true and \keyword{false} if it is
false.
\rSec2[expr.bit.and]{Bitwise AND operator}%
\indextext{expression!bitwise AND}%
\indextext{operator!bitwise}%
\indextext{operator!bitwise AND}%
\indextext{\idxcode{\&}|see{operator, bitwise AND}}%
\begin{bnf}
\nontermdef{and-expression}\br
equality-expression\br
and-expression \terminal{\&} equality-expression
\end{bnf}
\pnum
The \tcode{\&} operator groups left-to-right.
The operands shall be of integral or unscoped enumeration type.
The usual arithmetic conversions\iref{expr.arith.conv} are performed.
Given the coefficients $\tcode{x}_i$ and $\tcode{y}_i$
of the base-2 representation\iref{basic.fundamental}
of the converted operands \tcode{x} and \tcode{y},
the coefficient $\tcode{r}_i$
of the base-2 representation of the result \tcode{r}
is 1 if both $\tcode{x}_i$ and $\tcode{y}_i$ are 1, and 0 otherwise.
\begin{note}
The result is the bitwise \logop{and} function of the operands.
\end{note}
\rSec2[expr.xor]{Bitwise exclusive OR operator}%
\indextext{expression!bitwise exclusive OR}%
\indextext{operator!bitwise exclusive OR}%
\indextext{\idxcode{\caret}|see{operator, bitwise exclusive OR}}
\begin{bnf}
\nontermdef{exclusive-or-expression}\br
and-expression\br
exclusive-or-expression \terminal{\caret} and-expression
\end{bnf}
\pnum
The \tcode{\caret} operator groups left-to-right.
The operands shall be of integral or unscoped enumeration type.
The usual arithmetic conversions\iref{expr.arith.conv} are performed.
Given the coefficients $\tcode{x}_i$ and $\tcode{y}_i$
of the base-2 representation\iref{basic.fundamental}
of the converted operands \tcode{x} and \tcode{y},
the coefficient $\tcode{r}_i$
of the base-2 representation of the result \tcode{r}
is 1 if either (but not both) of $\tcode{x}_i$ and $\tcode{y}_i$ is 1,
and 0 otherwise.
\begin{note}
The result is the bitwise exclusive \logop{or} function of the operands.
\end{note}
\rSec2[expr.or]{Bitwise inclusive OR operator}%
\indextext{expression!bitwise inclusive OR}%
\indextext{operator!bitwise inclusive OR}%
\indextext{\idxcode{"|}|see{operator, bitwise inclusive OR}}
\begin{bnf}
\nontermdef{inclusive-or-expression}\br
exclusive-or-expression\br
inclusive-or-expression \terminal{|} exclusive-or-expression
\end{bnf}
\pnum
The \tcode{|} operator groups left-to-right.
The operands shall be of integral or unscoped enumeration type.
The usual arithmetic conversions\iref{expr.arith.conv} are performed.
Given the coefficients $\tcode{x}_i$ and $\tcode{y}_i$
of the base-2 representation\iref{basic.fundamental}
of the converted operands \tcode{x} and \tcode{y},
the coefficient $\tcode{r}_i$
of the base-2 representation of the result \tcode{r}
is 1 if at least one of $\tcode{x}_i$ and $\tcode{y}_i$ is 1, and 0 otherwise.
\begin{note}
The result is the bitwise inclusive \logop{or} function of the operands.
\end{note}
\rSec2[expr.log.and]{Logical AND operator}%
\indextext{expression!logical AND}%
\indextext{operator!logical AND}%
\indextext{\idxcode{\&\&}|see{operator, logical AND}}%
\begin{bnf}
\nontermdef{logical-and-expression}\br
inclusive-or-expression\br
logical-and-expression \terminal{\&\&} inclusive-or-expression
\end{bnf}
\pnum
The \tcode{\&\&} operator groups left-to-right. The operands are both
contextually converted to \keyword{bool}\iref{conv}.
The
result is \keyword{true} if both operands are \keyword{true} and
\keyword{false} otherwise. Unlike \tcode{\&}, \tcode{\&\&} guarantees
left-to-right evaluation: the second operand is not evaluated if the
first operand is \keyword{false}.
\pnum
The result is a \tcode{bool}.
\indextext{operator!side effects and logical AND}%
If the second expression is evaluated,
the first expression is sequenced before
the second expression\iref{intro.execution}.
\rSec2[expr.log.or]{Logical OR operator}%
\indextext{expression!logical OR}%
\indextext{operator!logical OR}%
\indextext{\idxcode{"|"|}|see{operator, logical OR}}%
\begin{bnf}
\nontermdef{logical-or-expression}\br
logical-and-expression\br
logical-or-expression \terminal{||} logical-and-expression
\end{bnf}
\pnum
The \tcode{||} operator groups left-to-right. The operands are both
contextually converted to \keyword{bool}\iref{conv}.
The result is
\keyword{true} if either of its operands is \keyword{true}, and
\keyword{false} otherwise. Unlike \tcode{|}, \tcode{||} guarantees
left-to-right evaluation; moreover, the second operand is not evaluated
if the first operand evaluates to \keyword{true}.
\pnum
The result is a \keyword{bool}.
\indextext{operator!side effects and logical OR}%
If the second expression is evaluated,
the first expression is sequenced before
the second expression\iref{intro.execution}.
\rSec2[expr.cond]{Conditional operator}%
\indextext{expression!conditional operator}%
\indextext{operator!conditional expression}%
\indextext{\idxcode{?:}|see{operator, conditional expression}}%
\begin{bnf}
\nontermdef{conditional-expression}\br
logical-or-expression\br
logical-or-expression \terminal{?} expression \terminal{:} assignment-expression
\end{bnf}
\pnum
Conditional expressions group right-to-left. The first expression is
contextually converted to \keyword{bool}\iref{conv}.
It is
evaluated and if it is \keyword{true}, the result of the conditional
expression is the value of the second expression, otherwise that of the
third expression. Only one of the second and third expressions is
evaluated.
The first expression is sequenced before
the second or third expression\iref{intro.execution}.
\pnum
If either the second or the third operand has type \keyword{void},
one of the following shall hold:
\begin{itemize}
\item
\indextext{conditional-expression!throw-expression in}%
The second or the third operand (but not both) is a (possibly
parenthesized) \grammarterm{throw-expression}\iref{expr.throw}; the result
is of the type and value category of the other.
The \grammarterm{conditional-expression}
is a bit-field if that operand is a bit-field.
\item Both the second and the third operands have type \keyword{void}; the
result is of type \keyword{void} and is a prvalue.
\begin{note}
This
includes the case where both operands are \grammarterm{throw-expression}{s}.
\end{note}
\end{itemize}
\pnum
Otherwise, if the second and third operand are glvalue bit-fields
of the same value category and
of types \cvqual{cv1} \tcode{T} and \cvqual{cv2} \tcode{T}, respectively,
the operands are considered to be of type \cv{} \tcode{T}
for the remainder of this subclause,
where \cv{} is the union of \cvqual{cv1} and \cvqual{cv2}.
\pnum
Otherwise, if the second and third operand have different types and
either has (possibly cv-qualified) class type, or if both
are glvalues of the same value category and the same type except for
cv-qualification, an attempt is made to
form an implicit conversion sequence\iref{over.best.ics} from
each of those operands to the type of the other.
\begin{note}
Properties such as access, whether an operand is a bit-field, or whether
a conversion function is deleted are ignored for that determination.
\end{note}
Attempts are made to form an implicit conversion sequence
from an operand expression \tcode{E1} of type \tcode{T1}
to a target type related to the type \tcode{T2}
of the operand expression \tcode{E2} as follows:
\begin{itemize}
\item If \tcode{E2} is an lvalue, the target type is
``lvalue reference to \tcode{T2}'',
but an implicit conversion sequence can only be formed
if the reference would bind directly\iref{dcl.init.ref}
to a glvalue.
\item If \tcode{E2} is an xvalue, the target type is
``rvalue reference to \tcode{T2}'',
but an implicit conversion sequence can only be formed
if the reference would bind directly.
\item If \tcode{E2} is a prvalue or if neither of the conversion sequences above can be
formed and at least one of the operands has (possibly cv-qualified) class type:
\begin{itemize}
\item if \tcode{T1} and \tcode{T2} are the same class type
(ignoring cv-qualification):
\begin{itemize}
\item
if \tcode{T2} is at least as cv-qualified as \tcode{T1},
the target type is \tcode{T2},
\item
otherwise, no conversion sequence is formed for this operand;
\end{itemize}
\item otherwise, if \tcode{T2} is a base class of \tcode{T1},
the target type is \cvqual{cv1} \tcode{T2}, where \cvqual{cv1}
denotes the cv-qualifiers of \tcode{T1};
\item otherwise, the target type is the type that \tcode{E2} would have
after applying the
lvalue-to-rvalue\iref{conv.lval},
array-to-pointer\iref{conv.array}, and
function-to-pointer\iref{conv.func}
standard conversions.
\end{itemize}
\end{itemize}
Using this process, it is determined whether an implicit conversion
sequence can be formed from the second operand
to the target type determined for the third operand, and vice versa,
with the following outcome:
\begin{itemize}
\item If both sequences can be formed, or one can be formed but it is the
ambiguous conversion sequence, the program is ill-formed.
\item If no conversion sequence can be formed, the operands are left unchanged
and further checking is performed as described below.
\item Otherwise, if exactly one conversion sequence can be formed,
that conversion is applied to the chosen operand
and the converted operand is used in place of the original operand for
the remainder of this subclause.
\begin{note}
The conversion might be ill-formed even if an implicit conversion
sequence could be formed.
\end{note}
\end{itemize}
\pnum
If the second and third operands are glvalues of the same value category
and have the same type, the
result is of that type and value category and it is a bit-field if the
second or the third operand is a bit-field, or if both are bit-fields.
\pnum
Otherwise, the result is a prvalue. If the second and third operands do
not have the same type, and either has (possibly cv-qualified) class
type, overload resolution is used to determine the conversions (if any)
to be applied to the operands\iref{over.match.oper,over.built}.
If the overload resolution fails, the program is ill-formed. Otherwise,
the conversions thus determined are applied, and the converted operands
are used in place of the original operands for the remainder of this
subclause.
\pnum
Array-to-pointer\iref{conv.array}
and function-to-pointer\iref{conv.func} standard conversions are
performed on the second and third operands. After those conversions, one
of the following shall hold:
\begin{itemize}
\item The second and third operands have the same type; the result is of
that type and the result is copy-initialized using the selected operand.
\item The second and third operands have arithmetic or enumeration type;
the usual arithmetic conversions\iref{expr.arith.conv} are performed to bring them to a common
type, and the result is of that type.
\item One or both of the second and third operands have pointer type;
lvalue-to-rvalue\iref{conv.lval},
pointer\iref{conv.ptr},
function pointer\iref{conv.fctptr}, and
qualification conversions\iref{conv.qual}
are performed to bring them to their
composite pointer type\iref{expr.type}. The result is of the composite
pointer type.
\item One or both of the second and third operands have pointer-to-member type;
lvalue-to-rvalue\iref{conv.lval},
pointer to member\iref{conv.mem},
function pointer\iref{conv.fctptr}, and
qualification conversions\iref{conv.qual}
are performed to bring them to their composite
pointer type\iref{expr.type}. The result is of the composite pointer type.
\item
Both the second and third operands have type \tcode{std::nullptr_t} or one has
that type and the other is a null pointer constant. The result is of type
\tcode{std::nullptr_t}.
\end{itemize}
\rSec2[expr.yield]{Yielding a value}%
\indextext{expression!yield}%
\indextext{\idxcode{co_yield}}%
\begin{bnf}
\nontermdef{yield-expression}\br
\keyword{co_yield} assignment-expression\br
\keyword{co_yield} braced-init-list
\end{bnf}
\pnum
A \grammarterm{yield-expression} shall appear only within a suspension context
of a function\iref{expr.await}.
Let \placeholder{e} be the operand of the \grammarterm{yield-expression} and
\placeholder{p} be an lvalue naming the promise object of the enclosing
coroutine\iref{dcl.fct.def.coroutine}, then the \grammarterm{yield-expression}
is equivalent to the expression
\tcode{\keyword{co_await} \placeholder{p}.yield_value(\placeholder{e})}.
\begin{example}
\begin{codeblock}
template
struct my_generator {
struct promise_type {
T current_value;
@\commentellip@
auto yield_value(T v) {
current_value = std::move(v);
return std::suspend_always{};
}
};
struct iterator { @\commentellip@ };
iterator begin();
iterator end();
};
my_generator> g1() {
for (int i = 0; i < 10; ++i) co_yield {i,i};
}
my_generator> g2() {
for (int i = 0; i < 10; ++i) co_yield make_pair(i,i);
}
auto f(int x = co_yield 5); // error: \grammarterm{yield-expression} outside of function suspension context
int a[] = { co_yield 1 }; // error: \grammarterm{yield-expression} outside of function suspension context
int main() {
auto r1 = g1();
auto r2 = g2();
assert(std::equal(r1.begin(), r1.end(), r2.begin(), r2.end()));
}
\end{codeblock}
\end{example}
\rSec2[expr.throw]{Throwing an exception}%
\indextext{expression!\idxcode{throw}}%
\indextext{exception handling!throwing}%
\indextext{\idxcode{throw}}%
%
\begin{bnf}
\nontermdef{throw-expression}\br
\keyword{throw} \opt{assignment-expression}
\end{bnf}
\pnum
A \grammarterm{throw-expression} is of type \keyword{void}.
\pnum
A \grammarterm{throw-expression} with an operand throws an
exception\iref{except.throw}.
The array-to-pointer\iref{conv.array} and function-to-pointer\iref{conv.func}
standard conversions are performed on the operand.
The type of the exception object is determined by removing
any top-level \grammarterm{cv-qualifier}{s} from the type of the
(possibly converted) operand.
The exception object is copy-initialized\iref{dcl.init.general}
from the (possibly converted) operand.
\pnum
\indextext{exception handling!rethrow}%
A
\grammarterm{throw-expression}
with no operand rethrows the currently handled exception\iref{except.handle}.
\indextext{exception handling!terminate called@\tcode{terminate} called}%
\indextext{\idxcode{terminate}!called}%
If no exception is presently being handled,
the function \tcode{std::terminate} is invoked\iref{except.terminate}.
Otherwise, the exception is reactivated with the existing exception object;
no new exception object is created.
The exception is no longer considered to be caught.
\begin{example}
An exception handler that cannot completely handle the exception itself
can be written like this:
\begin{codeblock}
try {
// ...
} catch (...) { // catch all exceptions
// respond (partially) to exception
throw; // pass the exception to some other handler
}
\end{codeblock}
\end{example}
\rSec2[expr.assign]{Assignment and compound assignment operators}%
\indextext{expression!assignment and compound assignment}
\pnum
\indextext{operator!assignment}%
\indextext{\idxcode{=}|see{assignment operator}}%
\indextext{operator!\idxcode{+=}}%
\indextext{operator!\idxcode{-=}}%
\indextext{operator!\idxcode{*=}}%
\indextext{operator!\idxcode{/=}}%
\indextext{operator!\idxcode{\%=}}%
\indextext{operator!\idxcode{>>=}}%
\indextext{operator!\idxcode{<<=}}%
\indextext{operator!\idxcode{\&=}}%
\indextext{operator!\idxcode{\caret=}}%
\indextext{operator!\idxcode{"|=}}%
The assignment operator (\tcode{=}) and the compound assignment
operators all group right-to-left.
\indextext{assignment!and lvalue}%
All
require a modifiable lvalue as their left operand; their result is an lvalue
of the type of the left operand, referring to the left operand. The result in all cases is a bit-field if
the left operand is a bit-field. In all cases, the assignment is
sequenced after the
\indextext{value computation}%
value computation of the right and left operands,
and before the
value computation of the assignment expression.
The right operand is sequenced before the left operand.
With
respect to an indeterminately-sequenced function call, the operation of
a compound assignment is a single evaluation.
\begin{note}
Therefore, a function call cannot intervene between the
lvalue-to-rvalue conversion and the side effect associated with any
single compound assignment operator.
\end{note}
\begin{bnf}
\nontermdef{assignment-expression}\br
conditional-expression\br
yield-expression\br
throw-expression\br
logical-or-expression assignment-operator initializer-clause
\end{bnf}
\begin{bnf}
\nontermdef{assignment-operator} \textnormal{one of}\br
\terminal{= *= /= \%= += -= >>= <<= \&= \caret= |=}
\end{bnf}
\pnum
In simple assignment (\tcode{=}),
let \tcode{V} be the result of the right operand;
the object referred to by the left operand is
modified\iref{defns.access} by replacing its value
with \tcode{V} or,
if the object is of integer type,
with the value congruent\iref{basic.fundamental} to \tcode{V}.
\pnum
\indextext{assignment!conversion by}%
If the right operand is an expression, it is implicitly
converted\iref{conv} to the cv-unqualified type of the left
operand.
\pnum
\indextext{reference!assignment to}%
When the left operand of an assignment operator
is a bit-field that cannot represent the value of the expression, the
resulting value of the bit-field is
\impldefplain{value of bit-field that cannot represent!assigned value}.
\pnum
An assignment whose left operand is of
a volatile-qualified type is deprecated\iref{depr.volatile.type}
unless the (possibly parenthesized) assignment is a discarded-value expression or
an unevaluated operand\iref{term.unevaluated.operand}.
\pnum
The behavior of an expression of the form \tcode{E1 \placeholder{op}= E2}
is equivalent to \tcode{E1 = E1 \placeholder{op} E2} except
that \tcode{E1} is evaluated only once.
\begin{note}
The object designated by \tcode{E1} is accessed twice.
\end{note}
For \tcode{+=} and \tcode{-=},
\tcode{E1} shall either have arithmetic type or be a pointer to a
possibly cv-qualified completely-defined object type. In all other
cases, \tcode{E1} shall have arithmetic type.
\pnum
If the value being stored in an object is read via another object that
overlaps in any way the storage of the first object, then the overlap shall be
exact and the two objects shall have the same type, otherwise the behavior is
undefined.
\begin{note}
This restriction applies to the relationship
between the left and right sides of the assignment operation; it is not a
statement about how the target of the assignment can be aliased in general.
See~\ref{basic.lval}.
\end{note}
\pnum
A \grammarterm{braced-init-list} $B$ may appear on the right-hand side of
\begin{itemize}
\item
an assignment to a scalar of type \tcode{T}, in which case $B$
shall have at most a single element.
The meaning of \tcode{x = $B$} is \tcode{x = t},
where \tcode{t} is an invented temporary variable
declared and initialized as \tcode{T t = $B$}.
\item
an assignment to an object of class type, in which case $B$
is passed as the argument to the assignment operator function selected by
overload resolution\iref{over.assign,over.match}.
\end{itemize}
\begin{example}
\begin{codeblock}
complex z;
z = { 1,2 }; // meaning \tcode{z.operator=(\{1,2\})}
z += { 1, 2 }; // meaning \tcode{z.operator+=(\{1,2\})}
int a, b;
a = b = { 1 }; // meaning \tcode{a=b=1;}
a = { 1 } = b; // syntax error
\end{codeblock}
\end{example}
\rSec2[expr.comma]{Comma operator}%
\indextext{expression!comma}%
\indextext{operator!comma}%
\indextext{comma operator|see{operator, comma}}%
\indextext{\idxcode{,}|see{operator, comma}}%
\indextext{sequencing operator|see{operator, comma}}%
\pnum
The comma operator groups left-to-right.
\begin{bnf}
\nontermdef{expression}\br
assignment-expression\br
expression \terminal{,} assignment-expression
\end{bnf}
A pair of expressions separated by a comma is evaluated left-to-right;
the left expression is
a discarded-value expression\iref{expr.prop}.
The left expression is sequenced before
the right expression\iref{intro.execution}.
\indextext{operator!side effects and comma}%
The type and value of the
result are the type and value of the right operand; the result is of the same
value category as its right operand, and is a bit-field if its
right operand is a bit-field.
\pnum
\begin{note}
In contexts where the comma token is given special meaning
(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}
\begin{codeblock}
f(a, (t=3, t+2), c);
\end{codeblock}
has three arguments, the second of which has the value
\tcode{5}.
\end{example}
\end{note}
\rSec1[expr.const]{Constant expressions}%
\indextext{expression!constant}
\pnum
Certain contexts require expressions that satisfy additional
requirements as detailed in this subclause; other contexts have different
semantics depending on whether or not an expression satisfies these requirements.
Expressions that satisfy these requirements,
assuming that copy elision\iref{class.copy.elision} is not performed,
are called
\indexdefn{expression!constant}%
\defnx{constant expressions}{constant expression}.
\begin{note}
Constant expressions can be evaluated
during translation.
\end{note}
\begin{bnf}
\nontermdef{constant-expression}\br
conditional-expression
\end{bnf}
\pnum
The \defnx{constituent values}{constituent value} of an object $o$ are
\begin{itemize}
\item
if $o$ has scalar type, the value of $o$;
\item
otherwise, the constituent values of any direct subobjects of $o$
other than inactive union members.
\end{itemize}
The \defnx{constituent references}{constituent reference} of an object $o$ are
\begin{itemize}
\item
any direct members of $o$ that have reference type, and
\item
the constituent references of any direct subobjects of $o$
other than inactive union members.
\end{itemize}
\pnum
The constituent values and constituent references of
a variable \tcode{x} are defined as follows:
\begin{itemize}
\item
If \tcode{x} declares an object,
the constituent values and references of that object are
constituent values and references of \tcode{x}.
\item
If \tcode{x} declares a reference,
that reference is a constituent reference of \tcode{x}.
\end{itemize}
For any constituent reference \tcode{r} of a variable \tcode{x},
if \tcode{r} is bound to a temporary object or subobject thereof
whose lifetime is extended to that of \tcode{r},
the constituent values and references of that temporary object
are also constituent values and references of \tcode{x}, recursively.
\pnum
An object $o$ is \defn{constexpr-referenceable} from a point $P$ if
\begin{itemize}
\item
$o$ has static storage duration, or
\item
$o$ has automatic storage duration, and, letting \tcode{v} denote
\begin{itemize}
\item
the variable corresponding to $o$'s complete object or
\item
the variable to whose lifetime that of $o$ is extended,
\end{itemize}
the smallest scope enclosing \tcode{v} and the smallest scope enclosing $P$
that are neither
\begin{itemize}
\item
block scopes nor
\item
function parameter scopes associated with
a \grammarterm{requirement-parameter-list}
\end{itemize}
are the same function parameter scope.
\end{itemize}
\begin{example}
\begin{codeblock}
struct A {
int m;
const int& r;
};
void f() {
static int sx;
thread_local int tx; // \tcode{tx} is never constexpr-referenceable
int ax;
A aa = {1, 2};
static A sa = {3, 4};
// The objects \tcode{sx}, \tcode{ax}, and \tcode{aa.m}, \tcode{sa.m}, and the temporaries to which \tcode{aa.r} and \tcode{sa.r} are bound, are constexpr-referenceable.
auto lambda = [] {
int ay;
// The objects \tcode{sx}, \tcode{sa.m}, and \tcode{ay} (but not \tcode{ax} or \tcode{aa}), and the
// temporary to which \tcode{sa.r} is bound, are constexpr-referenceable.
};
}
\end{codeblock}
\end{example}
\pnum
An object or reference \tcode{x} is
\defn{constexpr-representable} at a point $P$ if,
for each constituent value of \tcode{x} that points to or past an object $o$,
and for each constituent reference of \tcode{x} that refers to an object $o$,
$o$ is constexpr-referenceable from $P$.
\pnum
\indextext{contract evaluation semantics!ignore}
A variable \tcode{v} is \defn{constant-initializable} if
\begin{itemize}
\item
the full-expression of its initialization is a constant expression
when interpreted as a \grammarterm{constant-expression}
with all contract assertions
using the ignore evaluation semantic\iref{basic.contract.eval},
\begin{note}
Within this evaluation,
\tcode{std::is_constant_evaluated()}\iref{meta.const.eval}
returns \keyword{true}.
\end{note}
\begin{note}
The initialization, when evaluated,
can still evaluate contract assertions
with other evaluation semantics,
resulting in a diagnostic or ill-formed program
if a contract violation occurs.
\end{note}
\item
immediately after the initializing declaration of \tcode{v},
the object or reference \tcode{x} declared by \tcode{v}
is constexpr-representable, and
\item
if \tcode{x} has static or thread storage duration,
\tcode{x} is constexpr-representable at the nearest point
whose immediate scope is a namespace scope
that follows the initializing declaration of \tcode{v}.
\end{itemize}
\pnum
A constant-initializable variable is \defn{constant-initialized}
if either it has an initializer or
its type is const-default-constructible\iref{dcl.init.general}.
\begin{example}
\begin{codeblock}
void f() {
int ax = 0; // \tcode{ax} is constant-initialized
thread_local int tx = 0; // \tcode{tx} is constant-initialized
static int sx; // \tcode{sx} is not constant-initialized
static int& rss = sx; // \tcode{rss} is constant-initialized
static int& rst = tx; // \tcode{rst} is not constant-initialized
static int& rsa = ax; // \tcode{rsa} is not constant-initialized
thread_local int& rts = sx; // \tcode{rts} is constant-initialized
thread_local int& rtt = tx; // \tcode{rtt} is not constant-initialized
thread_local int& rta = ax; // \tcode{rta} is not constant-initialized
int& ras = sx; // \tcode{ras} is constant-initialized
int& rat = tx; // \tcode{rat} is not constant-initialized
int& raa = ax; // \tcode{raa} is constant-initialized
}
\end{codeblock}
\end{example}
\pnum
A variable is \defn{potentially-constant} if
it is constexpr or
it has reference or non-volatile const-qualified integral or enumeration type.
\pnum
A constant-initialized potentially-constant variable $V$ is
\defn{usable in constant expressions} at a point $P$ if
$V$'s initializing declaration $D$ is reachable from $P$ and
\begin{itemize}
\item $V$ is constexpr,
\item $V$ is not initialized to a TU-local value, or
\item $P$ is in the same translation unit as $D$.
\end{itemize}
An object or reference is
\defn{potentially usable in constant expressions} at point $P$ if it is
\begin{itemize}
\item
the object or reference declared by a variable
that is usable in constant expressions at $P$,
\item
a temporary object of non-volatile const-qualified literal type
whose lifetime is extended\iref{class.temporary}
to that of a variable that is usable in constant expressions at $P$,
\item
a template parameter object\iref{temp.param},
\item
a string literal object\iref{lex.string},
\item
a non-mutable subobject of any of the above, or
\item
a reference member of any of the above.
\end{itemize}
An object or reference is \defn{usable in constant expressions} at point $P$
if it is an object or reference
that is potentially usable in constant expressions at $P$ and
is constexpr-representable at $P$.
\begin{example}
\begin{codeblock}
struct A {
int* const & r;
};
void f(int x) {
constexpr A a = {&x};
static_assert(a.r == &x); // OK
[&] {
static_assert(a.r != nullptr); // error: \tcode{a.r} is not usable in constant expressions at this point
}();
}
\end{codeblock}
\end{example}
\pnum
An expression $E$ is a \defnadj{core constant}{expression}
unless the evaluation of $E$, following the rules of the abstract
machine\iref{intro.execution}, would evaluate one of the following:
\begin{itemize}
\item
\keyword{this}\iref{expr.prim.this}, except
\begin{itemize}
\item
in a constexpr function\iref{dcl.constexpr}
that is being evaluated as part of $E$ or
\item
when appearing as the \grammarterm{postfix-expression} of
an implicit or explicit class member access expression\iref{expr.ref};
\end{itemize}
\item
a control flow that passes through
a declaration of a block variable\iref{basic.scope.block} with
static\iref{basic.stc.static} or
thread\iref{basic.stc.thread} storage duration,
unless that variable is usable in constant expressions;
\begin{example}
\begin{codeblock}
constexpr char test() {
static const int x = 5;
static constexpr char c[] = "Hello World";
return *(c + x);
}
static_assert(' ' == test());
\end{codeblock}
\end{example}
\item
an invocation of a non-constexpr function;
\begin{footnote}
Overload resolution\iref{over.match}
is applied as usual.
\end{footnote}
\item
an invocation of an undefined constexpr function;
\item
an invocation of an instantiated constexpr function
that is not constexpr-suitable;
\item
an invocation of a virtual function\iref{class.virtual}
for an object whose dynamic type is constexpr-unknown;
\item
an expression that would exceed the implementation-defined
limits (see \ref{implimits});
\item
an operation that would have undefined or erroneous behavior
as specified in \ref{intro} through \ref{\lastcorechapter};
\begin{footnote}
This includes,
for example, signed integer overflow\iref{expr.pre}, certain
pointer arithmetic\iref{expr.add}, division by
zero\iref{expr.mul}, or certain shift operations\iref{expr.shift}.
\end{footnote}
\item
an lvalue-to-rvalue conversion\iref{conv.lval} unless
it is applied to
\begin{itemize}
\item
a glvalue of type \cv{}~\tcode{std::nullptr_t},
\item
a non-volatile glvalue that refers to an object that is
usable in constant expressions, or
\item
a non-volatile glvalue of literal type that refers to a non-volatile object
whose lifetime began within the evaluation of $E$;
\end{itemize}
\item
an lvalue-to-rvalue conversion
that is applied to a glvalue
that refers to a non-active member of a union or a subobject thereof;
\item
an lvalue-to-rvalue conversion that is applied to
an object with an indeterminate value\iref{basic.indet};
\item
an invocation of an implicitly-defined copy/move constructor or
copy/move assignment operator
for a union whose active member (if any) is mutable,
unless the lifetime of the union object began within the evaluation of $E$;
\item
in a \grammarterm{lambda-expression},
a reference to \keyword{this} or to a variable with
automatic storage duration defined outside that
\grammarterm{lambda-expression}, where
the reference would be an odr-use\iref{term.odr.use,expr.prim.lambda};
\begin{example}
\begin{codeblock}
void g() {
const int n = 0;
[=] {
constexpr int i = n; // OK, \tcode{n} is not odr-used here
constexpr int j = *&n; // error: \tcode{\&n} would be an odr-use of \tcode{n}
};
}
\end{codeblock}
\end{example}
\begin{note}
If the odr-use occurs in an invocation
of a function call operator of a closure type,
it no longer refers to \keyword{this} or to an enclosing
variable with automatic storage duration
due to the transformation\iref{expr.prim.lambda.capture}
of the \grammarterm{id-expression} into
an access of the corresponding data member.
\begin{example}
\begin{codeblock}
auto monad = [](auto v) { return [=] { return v; }; };
auto bind = [](auto m) {
return [=](auto fvm) { return fvm(m()); };
};
// OK to capture objects with automatic storage duration created during constant expression evaluation.
static_assert(bind(monad(2))(monad)() == monad(2)());
\end{codeblock}
\end{example}
\end{note}
\item
a conversion
from a prvalue \tcode{P} of type ``pointer to \cv{}~\keyword{void}''
to a type ``\cvqual{cv1} pointer to \tcode{T}'',
where \tcode{T} is not \cvqual{cv2}~\keyword{void},
unless \tcode{P}
is a null pointer value or
points to an object whose type is similar to \tcode{T};
\item
a \keyword{reinterpret_cast}\iref{expr.reinterpret.cast};
\item
a modification of an object\iref{expr.assign,expr.post.incr,expr.pre.incr}
unless it is applied to a non-volatile lvalue of literal type
that refers to a non-volatile object
whose lifetime began within the evaluation of $E$;
\item
an invocation of a destructor\iref{class.dtor} or a function call
whose \grammarterm{postfix-expression} names a pseudo-destructor\iref{expr.call},
in either case for an object whose lifetime did not begin within the evaluation of $E$;
\item
a \grammarterm{new-expression}\iref{expr.new},
unless either
\begin{itemize}
\item
the selected allocation function is
a replaceable global allocation function\iref{new.delete.single,new.delete.array} and
the allocated storage is deallocated within the evaluation of $E$, or
\item
the selected allocation function is
a non-allocating form\iref{new.delete.placement}
with an allocated type \tcode{T}, where
\begin{itemize}
\item
the placement argument to the \grammarterm{new-expression} points to
an object whose type is similar to \tcode{T}\iref{conv.qual} or,
if \tcode{T} is an array type,
to the first element of an object of a type similar to \tcode{T}, and
\item
the placement argument points to storage
whose duration began within the evaluation of $E$;
\end{itemize}
\end{itemize}
\item
a \grammarterm{delete-expression}\iref{expr.delete},
unless it deallocates a region of storage
allocated within the evaluation of $E$;
\item
a call to an instance of
\tcode{std::allocator::allocate}\iref{allocator.members},
unless the allocated storage is deallocated within the evaluation of $E$;
\item
a call to an instance of
\tcode{std::allocator::deallocate}\iref{allocator.members},
unless it deallocates a region of storage
allocated within the evaluation of $E$;
\item
a construction of an exception object,
unless the exception object and
all of its implicit copies created by invocations of
\tcode{std::current_exception} or \tcode{std::rethrow_exception}\iref{propagation}
are destroyed within the evaluation of $E$;
\item
an \grammarterm{await-expression}\iref{expr.await};
\item
a \grammarterm{yield-expression}\iref{expr.yield};
\item
a three-way comparison\iref{expr.spaceship},
relational\iref{expr.rel}, or equality\iref{expr.eq}
operator where the result is unspecified;
\item
a \keyword{dynamic_cast}\iref{expr.dynamic.cast} or
\keyword{typeid}\iref{expr.typeid} expression
on a glvalue that refers to an object
whose dynamic type is constexpr-unknown;
\item
a \tcode{dynamic_cast}\iref{expr.dynamic.cast} expression,
\tcode{typeid}\iref{expr.typeid} expression, or
\tcode{new-expression}\iref{expr.new}
that would throw an exception
where no definition of the exception type is reachable;
\item
an expression that would produce an injected declaration (see below),
unless $E$ is the corresponding expression of
a \grammarterm{consteval-block-declaration}\iref{dcl.pre};
\item
an \grammarterm{asm-declaration}\iref{dcl.asm};
\item
an invocation of the \libmacro{va_arg} macro\iref{cstdarg.syn};
\item
a non-constant library call\iref{defns.nonconst.libcall};
or
\item
a \keyword{goto} statement\iref{stmt.goto}.
\begin{note}
A \keyword{goto} statement introduced by equivalence\iref{stmt}
is not in scope.
For example, a \keyword{while} statement\iref{stmt.while}
can be executed during constant evaluation.
\end{note}
\end{itemize}
\pnum
It is
\impldef{whether an expression is a core constant expression}
whether $E$ is a core constant expression
if $E$ satisfies the constraints of a core constant expression, but
evaluation of $E$ has runtime-undefined behavior.
\pnum
It is unspecified whether $E$ is a core constant expression
if $E$ satisfies the constraints of a core constant expression, but
evaluation of $E$ would evaluate
\begin{itemize}
\item
an operation that has undefined behavior
as specified in \ref{library} through \ref{\lastlibchapter} or
\item
an invocation of the \libmacro{va_start} macro\iref{cstdarg.syn}.
\end{itemize}
\pnum
\begin{example}
\begin{codeblock}
int x; // not constant
struct A {
constexpr A(bool b) : m(b?42:x) { }
int m;
};
constexpr int v = A(true).m; // OK, constructor call initializes \tcode{m} with the value \tcode{42}
constexpr int w = A(false).m; // error: initializer for \tcode{m} is \tcode{x}, which is non-constant
constexpr int f1(int k) {
constexpr int x = k; // error: \tcode{x} is not initialized by a constant expression
// because lifetime of \tcode{k} began outside the initializer of \tcode{x}
return x;
}
constexpr int f2(int k) {
int x = k; // OK, not required to be a constant expression
// because \tcode{x} is not \keyword{constexpr}
return x;
}
constexpr int incr(int &n) {
return ++n;
}
constexpr int g(int k) {
constexpr int x = incr(k); // error: \tcode{incr(k)} is not a core constant expression
// because lifetime of \tcode{k} began outside the expression \tcode{incr(k)}
return x;
}
constexpr int h(int k) {
int x = incr(k); // OK, \tcode{incr(k)} is not required to be a core constant expression
return x;
}
constexpr int y = h(1); // OK, initializes \tcode{y} with the value \tcode{2}
// \tcode{h(1)} is a core constant expression because
// the lifetime of \tcode{k} begins inside \tcode{h(1)}
\end{codeblock}
\end{example}
\pnum
For the purposes of determining
whether an expression $E$ is a core constant expression,
the evaluation of the body of a member function of \tcode{std::allocator}
as defined in \ref{allocator.members}, where \tcode{T} is a literal type,
is ignored.
\pnum
For the purposes of determining whether $E$ is a core constant expression,
the evaluation of a call to
a trivial copy/move constructor or copy/move assignment operator of a union
is considered to copy/move the active member of the union, if any.
\begin{note}
The copy/move of the active member is trivial.
\end{note}
\pnum
For the purposes of determining whether $E$ is a core constant expression,
the evaluation of an \grammarterm{id-expression}
that names a structured binding \tcode{v}\iref{dcl.struct.bind} has the
following semantics:
\begin{itemize}
\item
If \tcode{v} is an lvalue referring to the object bound to an invented reference \tcode{r},
the behavior is as if \tcode{r} were nominated.
\item
Otherwise, if \tcode{v} names an array element or class member,
the behavior is that of
evaluating \tcode{$e$[$i$]} or \tcode{$e$.$m$}, respectively,
where $e$ is the name of the variable
initialized from the initializer of the structured binding declaration, and
$i$ is the index of the element referred to by \tcode{v} or
$m$ is the name of the member referred to by \tcode{v}, respectively.
\end{itemize}
\begin{example}
\begin{codeblock}
#include
struct S {
mutable int m;
constexpr S(int m): m(m) {}
virtual int g() const;
};
void f(std::tuple t) {
auto [r] = t;
static_assert(r.g() >= 0); // error: dynamic type is constexpr-unknown
constexpr auto [m] = S(1);
static_assert(m == 1); // error: lvalue-to-rvalue conversion on mutable
// subobject \tcode{e.m}, where \tcode{e} is a constexpr object of type \tcode{S}
using A = int[2];
constexpr auto [v0, v1] = A{2, 3};
static_assert(v0 + v1 == 5); // OK, equivalent to \tcode{e[0] + e[1]} where \tcode{e} is a constexpr array
}
\end{codeblock}
\end{example}
\pnum
During the evaluation of an expression $E$ as a core constant expression,
all \grammarterm{id-expression}s, \grammarterm{splice-expression}s, and
uses of \tcode{*\keyword{this}}
that refer to an object or reference
whose lifetime did not begin with the evaluation of $E$
are treated as referring to a specific instance of that object or reference
whose lifetime and that of all subobjects (including all union members)
includes the entire constant evaluation.
For such an object that is not usable in constant expressions,
the dynamic type of the object is \defn{constexpr-unknown}.
For such a reference that is not usable in constant expressions,
the reference is treated as binding to
an unspecified object of the referenced type
whose lifetime and that of all subobjects includes
the entire constant evaluation and whose dynamic type is constexpr-unknown.
\begin{example}
\begin{codeblock}
template
constexpr size_t array_size(T (&)[N]) {
return N;
}
void use_array(int const (&gold_medal_mel)[2]) {
constexpr auto gold = array_size(gold_medal_mel); // OK
}
constexpr auto olympic_mile() {
const int ledecky = 1500;
return []{ return ledecky; };
}
static_assert(olympic_mile()() == 1500); // OK
struct Swim {
constexpr int phelps() { return 28; }
virtual constexpr int lochte() { return 12; }
int coughlin = 12;
};
constexpr int how_many(Swim& swam) {
Swim* p = &swam;
return (p + 1 - 1)->phelps();
}
void splash(Swim& swam) {
static_assert(swam.phelps() == 28); // OK
static_assert((&swam)->phelps() == 28); // OK
Swim* pswam = &swam;
static_assert(pswam->phelps() == 28); // error: lvalue-to-rvalue conversion on a pointer
// not usable in constant expressions
static_assert(how_many(swam) == 28); // OK
static_assert(Swim().lochte() == 12); // OK
static_assert(swam.lochte() == 12); // error: invoking virtual function on reference
// with constexpr-unknown dynamic type
static_assert(swam.coughlin == 12); // error: lvalue-to-rvalue conversion on an object
// not usable in constant expressions
}
extern Swim dc;
extern Swim& trident;
constexpr auto& sandeno = typeid(dc); // OK, can only be \tcode{typeid(Swim)}
constexpr auto& gallagher = typeid(trident); // error: constexpr-unknown dynamic type
\end{codeblock}
\end{example}
\pnum
An object \tcode{a} is said to have \defnadj{constant}{destruction} if
\begin{itemize}
\item
it is not of class type nor (possibly multidimensional) array thereof, or
\item
it is of class type or (possibly multidimensional) array thereof,
that class type has a constexpr destructor\iref{dcl.constexpr}, and
for a hypothetical expression $E$
whose only effect is to destroy \tcode{a},
$E$ would be a core constant expression
if the lifetime of \tcode{a} and its non-mutable subobjects
(but not its mutable subobjects) were considered to start within $E$.
\end{itemize}
\pnum
An \defnadj{integral constant}{expression}
is an expression of integral or
unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression.
\begin{note}
Such expressions can be
used as bit-field lengths\iref{class.bit}, as enumerator
initializers if the underlying type is not fixed\iref{dcl.enum},
and as alignments\iref{dcl.align}.
\end{note}
\pnum
If an expression of literal class type is used in a context where an
integral constant expression is required, then that expression is
contextually implicitly converted\iref{conv} to an integral or unscoped
enumeration type
and the selected conversion function shall be \keyword{constexpr}.
\begin{example}
\begin{codeblock}
struct A {
constexpr A(int i) : val(i) { }
constexpr operator int() const { return val; }
constexpr operator long() const { return 42; }
private:
int val;
};
constexpr A a = alignof(int);
alignas(a) int n; // error: ambiguous conversion
struct B { int n : a; }; // error: ambiguous conversion
\end{codeblock}
\end{example}
\pnum
A \defnadj{converted constant}{expression}
of type \tcode{T} is an
expression, implicitly converted to type \tcode{T}, where
the converted expression is a constant expression and the
implicit conversion sequence contains only
\begin{itemize}
\item user-defined conversions,
\item lvalue-to-rvalue conversions\iref{conv.lval},
\item array-to-pointer conversions\iref{conv.array},
\item function-to-pointer conversions\iref{conv.func},
\item qualification conversions\iref{conv.qual},
\item integral promotions\iref{conv.prom},
\item integral conversions\iref{conv.integral} other than narrowing conversions\iref{dcl.init.list},
\item floating-point promotions\iref{conv.fpprom},
\item floating-point conversions\iref{conv.double} where
the source value can be represented exactly in the destination type,
\item null pointer conversions\iref{conv.ptr} from \tcode{std::nullptr_t},
\item null member pointer conversions\iref{conv.mem} from \tcode{std::nullptr_t}, and
\item function pointer conversions\iref{conv.fctptr},
\end{itemize}
and where the reference binding (if any) binds directly.
\begin{note}
Such expressions can be used in \keyword{new}
expressions\iref{expr.new}, as case expressions\iref{stmt.switch},
as enumerator initializers if the underlying type is
fixed\iref{dcl.enum}, as array bounds\iref{dcl.array}, and
as constant template
arguments\iref{temp.arg}.
\end{note}
\indextext{contextually converted constant expression of type \tcode{bool}|see{conversion, contextual}}%
\indextext{conversion!contextual to constant expression of type \tcode{bool}}%
A \term{contextually converted constant expression of type \tcode{bool}} is
an expression, contextually converted to \keyword{bool}\iref{conv},
where the converted expression is a constant expression and
the conversion sequence contains only the conversions above.
\pnum
A \defnadj{constant}{expression} is either
\begin{itemize}
\item
a glvalue core constant expression $E$ for which
\begin{itemize}
\item
$E$ refers to a non-immediate function,
\item
$E$ designates an object \tcode{o}, and
if the complete object of \tcode{o} is of consteval-only type then so is $E$,
\begin{example}
\begin{codeblock}
struct Base { };
struct Derived : Base { std::meta::info r; };
consteval const Base& fn(const Derived& derived) { return derived; }
constexpr Derived obj{.r=^^::}; // OK
constexpr const Derived& d = obj; // OK
constexpr const Base& b = fn(obj); // error: not a constant expression because \tcode{Derived}
// is a consteval-only type but \tcode{Base} is not.
\end{codeblock}
\end{example}
\end{itemize}
or
\item
a prvalue core constant expression whose result object\iref{basic.lval}
satisfies the following constraints:
\begin{itemize}
\item
each constituent reference refers to an object or a non-immediate function,
\item
no constituent value of scalar type is an indeterminate or erroneous value\iref{basic.indet},
\item
no constituent value of pointer type is a pointer to an immediate function or
an invalid pointer value\iref{basic.compound},
\item
no constituent value of pointer-to-member type designates an immediate function, and
\item
unless the value is of consteval-only type,
\begin{itemize}
\item
no constituent value of pointer-to-member type points to
a direct member of a consteval-only class type,
\item
no constituent value of pointer type points to or past an object
whose complete object is of consteval-only type, and
\item
no constituent reference refers to an object
whose complete object is of consteval-only type.
\end{itemize}
\end{itemize}
\end{itemize}
\begin{note}
A glvalue core constant expression
that either refers to or points to an unspecified object
is not a constant expression.
\end{note}
\begin{example}
\begin{codeblock}
consteval int f() { return 42; }
consteval auto g() { return f; }
consteval int h(int (*p)() = g()) { return p(); }
constexpr int r = h(); // OK
constexpr auto e = g(); // error: a pointer to an immediate function is
// not a permitted result of a constant expression
struct S {
int x;
constexpr S() {}
};
int i() {
constexpr S s; // error: \tcode{s.x} has erroneous value
}
\end{codeblock}
\end{example}
\pnum
\recommended
Implementations should provide consistent results of floating-point evaluations,
irrespective of whether the evaluation is performed
during translation or during program execution.
\begin{note}
Since this document
imposes no restrictions on the accuracy of floating-point operations, it is unspecified whether the
evaluation of a floating-point expression during translation yields the same result as the
evaluation of the same expression (or the same operations on the same values) during program
execution.
\begin{example}
\begin{codeblock}
bool f() {
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
return sizeof(array) == size;
}
\end{codeblock}
It is unspecified whether the value of \tcode{f()} will be \tcode{true} or \tcode{false}.
\end{example}
\end{note}
\pnum
An expression or conversion is in an \defn{immediate function context}
if it is potentially evaluated and either:
\begin{itemize}
\item
its innermost enclosing non-block scope is
a function parameter scope of an immediate function,
\item
it is a subexpression of a manifestly constant-evaluated expression
or conversion, or
\item
its enclosing statement is enclosed\iref{stmt.pre} by
the \grammarterm{compound-statement} of a consteval if statement\iref{stmt.if}.
\end{itemize}
An invocation is an \defn{immediate invocation}
if it is a potentially-evaluated explicit or implicit invocation of
an immediate function and
is not in an immediate function context.
An aggregate initialization is an immediate invocation
if it evaluates a default member initializer
that has a subexpression that is an immediate-escalating expression.
\pnum
\indexdefn{expression!immediate-escalating}%
\indexdefn{conversion!immediate-escalating}%
\indexdefn{immediate-escalating!expression|see{expression, immediate-escalating}}%
\indexdefn{immediate-escalating!conversion|see{conversion, immediate-escalating}}%
A potentially-evaluated expression or conversion is \defn{immediate-escalating}
if it is neither initially in an immediate function context
nor a subexpression of an immediate invocation, and
\begin{itemize}
\item
it is an \grammarterm{id-expression} or \grammarterm{splice-expression}
that designates an immediate function,
\item
it is an immediate invocation that is not a constant expression, or
\item
it is of consteval-only type\iref{basic.types.general}.
\end{itemize}
\pnum
\indexdefn{immediate-escalating!function|see{function, immediate-escalating}}%
An \defnx{immediate-escalating}{function!immediate-escalating} function is
\begin{itemize}
\item
the call operator of a lambda that is not declared
with the \keyword{consteval} specifier,
\item
a defaulted special member function
that is not declared with the \keyword{consteval} specifier, or
\item
a function that results from the instantiation
of a templated entity defined with the \keyword{constexpr} specifier.
\end{itemize}
An immediate-escalating expression shall appear only
in an immediate-escalating function.
\pnum
An \defnadj{immediate}{function} is a function or constructor that is either
\begin{itemize}
\item
declared with the \keyword{consteval} specifier, or
\item
an immediate-escalating function \tcode{\placeholder{F}}
whose function body contains either
\begin{itemize}
\item an immediate-escalating expression or
\item a definition of a non-constexpr variable with consteval-only type
\end{itemize}
whose innermost enclosing non-block scope
is \tcode{\placeholder{F}}'s function parameter scope.
\begin{tailnote}
Default member initializers used to initialize
a base or member subobject\iref{class.base.init}
are considered to be part of the function body\iref{dcl.fct.def.general}.
\end{tailnote}
\end{itemize}
\begin{example}
\begin{codeblock}
consteval int id(int i) { return i; }
constexpr char id(char c) { return c; }
template
constexpr int f(T t) {
return t + id(t);
}
auto a = &f; // OK, \tcode{f} is not an immediate function
auto b = &f; // error: \tcode{f} is an immediate function
static_assert(f(3) == 6); // OK
template
constexpr int g(T t) { // \tcode{g} is not an immediate function
return t + id(42); // because \tcode{id(42)} is already a constant
}
template
constexpr bool is_not(T t, F f) {
return not f(t);
}
consteval bool is_even(int i) { return i % 2 == 0; }
static_assert(is_not(5, is_even)); // OK
int x = 0;
template
constexpr T h(T t = id(x)) { // \tcode{h} is not an immediate function
// \tcode{id(x)} is not evaluated when parsing the default argument\iref{dcl.fct.default,temp.inst}
return t;
}
template
constexpr T hh() { // \tcode{hh} is an immediate function because of the invocation
return h(); // of the immediate function \tcode{id} in the default argument of \tcode{h}
}
int i = hh(); // error: \tcode{hh()} is an immediate-escalating expression
// outside of an immediate-escalating function
struct A {
int x;
int y = id(x);
};
template
constexpr int k(int) { // \tcode{k} is not an immediate function because \tcode{A(42)} is a
return A(42).y; // constant expression and thus not immediate-escalating
}
constexpr int l(int c) pre(c >= 2) {
return (c % 2 == 0) ? c / 0 : c;
}
const int i0 = l(0); // dynamic initialization; contract violation or undefined behavior
const int i1 = l(1); // static initialization; value of \tcode{1} or contract violation at compile time
const int i2 = l(2); // dynamic initialization; undefined behavior
const int i3 = l(3); // static initialization; value of \tcode{3}
\end{codeblock}
\end{example}
\pnum
An expression or conversion is \defn{manifestly constant-evaluated}
if it is:
\begin{itemize}
\item a \grammarterm{constant-expression}, or
\item the condition of a constexpr if statement\iref{stmt.if}, or
\item an immediate invocation, or
\item the result of substitution into an atomic constraint expression
to determine whether it is satisfied\iref{temp.constr.atomic}, or
\item the initializer of a variable
that is usable in constant expressions or
has constant initialization\iref{basic.start.static}.
\begin{footnote}
Testing this condition
can involve a trial evaluation of its initializer,
with evaluations of contract assertions
using the ignore evaluation semantic\iref{basic.contract.eval},
as described above.
\end{footnote}
\begin{example}
\begin{codeblock}
template struct X {};
X x; // type \tcode{X}
int y;
const int a = std::is_constant_evaluated() ? y : 1; // dynamic initialization to 1
double z[a]; // error: \tcode{a} is not usable
// in constant expressions
const int b = std::is_constant_evaluated() ? 2 : y; // static initialization to 2
int c = y + (std::is_constant_evaluated() ? 2 : y); // dynamic initialization to \tcode{y+y}
constexpr int f() {
const int n = std::is_constant_evaluated() ? 13 : 17; // \tcode{n} is 13
int m = std::is_constant_evaluated() ? 13 : 17; // \tcode{m} can be 13 or 17 (see below)
char arr[n] = {}; // char[13]
return m + sizeof(arr);
}
int p = f(); // \tcode{m} is 13; initialized to 26
int q = p + f(); // \tcode{m} is 17 for this call; initialized to 56
\end{codeblock}
\end{example}
\end{itemize}
\begin{note}
Except for a \grammarterm{static_assert-message},
a manifestly constant-evaluated expression
is evaluated even in an unevaluated operand\iref{term.unevaluated.operand}.
\end{note}
\pnum
The evaluation of an expression can introduce
one or more \defnadjx{injected}{declarations}{declaration}.
The evaluation is said to \defn{produce} the declarations.
\begin{note}
An invocation of
the library function template \tcode{std::meta::define_aggregate}
produces an injected declaration\iref{meta.reflection.define.aggregate}.
\end{note}
Each such declaration has
\begin{itemize}
\item
an associated \defnadj{synthesized}{point},
which follows the last non-synthesized program point
in the translation unit containing that declaration, and
\item
an associated \defnadj{characteristic}{sequence} of values.
\end{itemize}
\begin{note}
Special rules concerning reachability
apply to synthesized points\iref{module.reach}.
\end{note}
\begin{note}
The program is ill-formed
if injected declarations with different characteristic sequences
define the same entity in different translation units\iref{basic.def.odr}.
\end{note}
\pnum
A member of an entity defined by an injected declaration
shall not have a name reserved to the implementation\iref{lex.name};
no diagnostic is required.
\pnum
Let $C$ be a \grammarterm{consteval-block-declaration},
the evaluation of whose corresponding expression
produces an injected declaration for an entity $E$.
The program is ill-formed if either
\begin{itemize}
\item
$C$ is enclosed by a scope associated with $E$ or
\item
letting $P$ be a point whose immediate scope is that to which $E$ belongs,
there is a function parameter scope or class scope
that encloses exactly one of $C$ or $P$.
\end{itemize}
\begin{example}
\begin{codeblock}
struct S0 {
consteval {
std::meta::define_aggregate(^^S0, {}); // error: scope associated with \tcode{S0} encloses the consteval block
}
};
struct S1;
consteval { std::meta::define_aggregate(^^S1, {}); } // OK
template consteval void tfn1() {
std::meta::define_aggregate(R, {});
}
struct S2;
consteval { tfn1<^^S2>(); } // OK
template consteval void tfn2() {
consteval { std::meta::define_aggregate(R, {}); }
}
struct S3;
consteval { tfn2<^^S3>(); }
// error: function parameter scope of \tcode{tfn2<\caret\caret S3>} intervenes between the declaration of \tcode{S3}
// and the consteval block that produces the injected declaration
template struct TCls {
struct S4;
static void sfn() requires ([] {
consteval { std::meta::define_aggregate(^^S4, {}); }
return true;
}()) { }
};
consteval { TCls::sfn(); } // error: \tcode{TCls::S4} is not enclosed by \grammarterm{requires-clause} lambda
struct S5;
struct Cls {
consteval { std::meta::define_aggregate(^^S5, {}); } // error: \tcode{S5} is not enclosed by class \tcode{Cls}
};
struct S6;
consteval { // \#1
struct S7; // local class
std::meta::define_aggregate(^^S7, {}); // error: consteval block \#1 does not enclose itself,
// but encloses \tcode{S7}
consteval { // \#2
std::meta::define_aggregate(^^S6, {}); // error: consteval block \#1 encloses
// consteval block \#2 but not \tcode{S6}
std::meta::define_aggregate(^^S7, {}); // OK, consteval block \#1 encloses both \#2 and \tcode{S7}
}
}
\end{codeblock}
\end{example}
\pnum
The \defn{evaluation context} is a set of program points
that determines the behavior of certain functions
used for reflection\iref{meta.reflection}.
During the evaluation $V$ of an expression $E$ as a core constant expression,
the evaluation context of an evaluation $X$\iref{intro.execution}
consists of the following points:
\begin{itemize}
\item
The program point $\textit{EVAL-PT}(L)$,
where $L$ is the point at which $E$ appears, and
where $\textit{EVAL-PT}(P)$, for a point $P$,
is a point $R$ determined as follows:
\begin{itemize}
\item
If a potentially-evaluated subexpression\iref{intro.execution} of
a default member initializer $I$ appears at $P$, and
a (possibly aggregate) initialization during $V$ is using $I$,
then $R$ is $\textit{EVAL-PT}(Q)$
where $Q$ is the point at which that initialization appears.
\item
Otherwise, if a potentially-evaluated subexpression of
a default argument\iref{dcl.fct.default} appears at $P$, and
an invocation of a function\iref{expr.call} during $V$
is using that default argument,
then $R$ is $\textit{EVAL-PT}(Q)$
where $Q$ is the point at which that invocation appears.
\item
Otherwise, $R$ is $P$.
\end{itemize}
\item
Each synthesized point corresponding to an injected declaration produced by
any evaluation sequenced before $X$\iref{intro.execution}.
\end{itemize}
\pnum
\indextext{expression!potentially constant evaluated}%
An expression or conversion is \defn{potentially constant evaluated}
if it is:
\begin{itemize}
\item
a manifestly constant-evaluated expression,
\item
a potentially-evaluated expression\iref{basic.def.odr},
\item
an immediate subexpression of a \grammarterm{braced-init-list},
\begin{footnote}
In some cases, constant evaluation is needed to determine whether a narrowing conversion is performed\iref{dcl.init.list}.
\end{footnote}
\item
an expression of the form \tcode{\&} \grammarterm{cast-expression}
that occurs within a templated entity,
\begin{footnote}
In some cases, constant evaluation is needed to determine whether such an expression is value-dependent\iref{temp.dep.constexpr}.
\end{footnote}
or
\item
a potentially-evaluated subexpression\iref{intro.execution} of one of the above.
\end{itemize}
\indextext{function!needed for constant evaluation}%
\indextext{variable!needed for constant evaluation}%
A function or variable is
\defn{needed for constant evaluation}
if it is:
\begin{itemize}
\item
a constexpr function that is named by an expression\iref{basic.def.odr}
that is potentially constant evaluated, or
\item
a potentially-constant variable named by a potentially constant evaluated expression.
\end{itemize}
\indextext{expression|)}