Skip to content

Commit adcdd79

Browse files
burblebeetkoeppe
authored andcommitted
CWG2765 Address comparisons between potentially non-unique objects during constant evaluation
1 parent 5d6e9c8 commit adcdd79

3 files changed

Lines changed: 65 additions & 9 deletions

File tree

source/basic.tex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5985,6 +5985,28 @@
59855985
alignment requirement.
59865986
\end{note}
59875987

5988+
\pnum
5989+
A pointer value
5990+
pointing to a potentially non-unique object $O$\iref{intro.object} is
5991+
\indextext{value!associated with an evaluation}%
5992+
\defn{associated with} the evaluation of
5993+
\begin{itemize}
5994+
\item
5995+
the string-literal\iref{lex.string} that resulted in the string literal object,
5996+
\item
5997+
the initializer list\iref{dcl.init.list} that resulted in the backing array,
5998+
or
5999+
\item
6000+
the initialization of
6001+
the template parameter object\iref{temp.arg.nontype, meta.define.static}
6002+
\end{itemize}
6003+
that is $O$ or of which $O$ is a subobject.
6004+
\begin{note}
6005+
A pointer value obtained by pointer arithmetic\iref{expr.add}
6006+
from a pointer value associated with an evaluation $E$
6007+
is also associated with $E$.
6008+
\end{note}
6009+
59886010
\pnum
59896011
A pointer value $P$ is
59906012
\indextext{value!valid in the context of an evaluation}%

source/expressions.tex

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8329,6 +8329,19 @@
83298329

83308330
\rSec2[expr.const.core]{Core constant expressions}
83318331

8332+
\pnum
8333+
\indextext{type!constexpr-unknown representation}%
8334+
A type has \defnadj{constexpr-unknown}{representation} if it
8335+
\begin{itemize}
8336+
\item is a union,
8337+
\item is a pointer or pointer-to-member type,
8338+
\item is volatile-qualified,
8339+
\item is a class type with a non-static data member of reference type, or
8340+
\item
8341+
has a base class or a non-static member whose
8342+
type has constexpr-unknown representation.
8343+
\end{itemize}
8344+
83328345
\pnum
83338346
An expression $E$ is a \defnadj{core constant}{expression}
83348347
unless the evaluation of $E$, following the rules of the abstract
@@ -8556,6 +8569,34 @@
85568569
relational\iref{expr.rel}, or equality\iref{expr.eq}
85578570
operator where the result is unspecified;
85588571

8572+
\item
8573+
an equality operator comparing pointers to potentially non-unique objects,
8574+
if the pointer values of the two operands
8575+
are associated with different evaluations\iref{basic.compound} and either
8576+
they can both point to the same offset within
8577+
the same potentially non-unique object or
8578+
one of them points to an object whose
8579+
type has constexpr-unknown representation;
8580+
\begin{example}
8581+
\begin{codeblock}
8582+
constexpr const char *f() { return "foo"; }
8583+
8584+
constexpr bool b1 = +"foo" == "foo"; // error: non-constant
8585+
constexpr bool b2 = f() == f(); // error: non-constant
8586+
constexpr const char *p = f();
8587+
constexpr bool b3 = p == p; // OK, value of \tcode{b3} is \tcode{true}
8588+
constexpr bool b4 = "xfoo" + 1 == "foo\0y"; // error: non-constant; string literal
8589+
// object could contain \tcode{"xfoo\textbackslash{}0y"}
8590+
constexpr bool b5 = "foo" == "bar" + 0; // OK, value of \tcode{b5} is \tcode{false}
8591+
constexpr bool b6 = (const char*)"foo" == "oo"; // OK, value of \tcode{b6} is \tcode{false}; offsets would
8592+
// be different in a merged string literal object
8593+
8594+
constexpr std::initializer_list<int *> il1 = { (int *)nullptr };
8595+
constexpr std::initializer_list<unsigned long> il2 = { 0 };
8596+
constexpr bool b7 = il1.begin() == (void *)il2.begin(); // error: non-constant
8597+
\end{codeblock}
8598+
\end{example}
8599+
85598600
\item
85608601
a \keyword{dynamic_cast}\iref{expr.dynamic.cast} or
85618602
\keyword{typeid}\iref{expr.typeid} expression

source/utilities.tex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15943,15 +15943,8 @@
1594315943

1594415944
\pnum
1594515945
\constantwhen
15946-
\tcode{To}, \tcode{From}, and the types of all subobjects
15947-
of \tcode{To} and \tcode{From} are types \tcode{T} such that:
15948-
\begin{itemize}
15949-
\item \tcode{is_union_v<T>} is \tcode{false};
15950-
\item \tcode{is_pointer_v<T>} is \tcode{false};
15951-
\item \tcode{is_member_pointer_v<T>} is \tcode{false};
15952-
\item \tcode{is_volatile_v<T>} is \tcode{false}; and
15953-
\item \tcode{T} has no non-static data members of reference type.
15954-
\end{itemize}
15946+
Neither \tcode{To} nor \tcode{From}
15947+
has constexpr-unknown representation\iref{expr.const}.
1595515948

1595615949
\pnum
1595715950
\returns

0 commit comments

Comments
 (0)