Skip to content

Commit 77dfb1d

Browse files
authored
Merge 2025-02 CWG Motion 8
P2786R13 Trivial Relocatability for C++26
2 parents bb3d30f + 5ddffed commit 77dfb1d

File tree

10 files changed

+341
-27
lines changed

10 files changed

+341
-27
lines changed

source/basic.tex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5030,6 +5030,14 @@
50305030
Scalar types, trivially copyable class types\iref{class.prop},
50315031
arrays of such types, and cv-qualified versions of these
50325032
types are collectively called \defnadjx{trivially copyable}{types}{type}.
5033+
\label{term.trivially.relocatable.type}%
5034+
Scalar types, trivially relocatable class types\iref{class.prop},
5035+
arrays of such types, and cv-qualified versions of these
5036+
types are collectively called \defnadjx{trivially relocatable}{types}{type}.
5037+
\label{term.replaceable.type}%
5038+
Cv-unqualified scalar types, replaceable class types\iref{class.prop}, and
5039+
arrays of such types are collectively called
5040+
\defnadjx{replaceable}{types}{type}.
50335041
\label{term.standard.layout.type}%
50345042
Scalar types, standard-layout class
50355043
types\iref{class.prop}, arrays of such types, and

source/classes.tex

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
\begin{bnf}
3838
\nontermdef{class-head}\br
39-
class-key \opt{attribute-specifier-seq} class-head-name \opt{class-virt-specifier} \opt{base-clause}\br
39+
class-key \opt{attribute-specifier-seq} class-head-name \opt{class-property-specifier-seq} \opt{base-clause}\br
4040
class-key \opt{attribute-specifier-seq} \opt{base-clause}
4141
\end{bnf}
4242

@@ -46,8 +46,15 @@
4646
\end{bnf}
4747

4848
\begin{bnf}
49-
\nontermdef{class-virt-specifier}\br
50-
\keyword{final}
49+
\nontermdef{class-property-specifier-seq}\br
50+
class-property-specifier \opt{class-property-specifier-seq}
51+
\end{bnf}
52+
53+
\begin{bnf}
54+
\nontermdef{class-property-specifier}\br
55+
\keyword{final}\br
56+
\keyword{trivially_relocatable_if_eligible}\br
57+
\keyword{replaceable_if_eligible}
5158
\end{bnf}
5259

5360
\begin{bnf}
@@ -120,12 +127,13 @@
120127
\end{note}
121128

122129
\pnum
123-
If a class is marked with the \grammarterm{class-virt-specifier} \tcode{final} and it appears
124-
as a \grammarterm{class-or-decltype} in a \grammarterm{base-clause}\iref{class.derived},
125-
the program is ill-formed. Whenever a
126-
\grammarterm{class-key} is followed by a \grammarterm{class-head-name}, the
127-
\grammarterm{identifier} \tcode{final}, and a colon or left brace, \tcode{final} is
128-
interpreted as a \grammarterm{class-virt-specifier}.
130+
Each \grammarterm{class-property-specifier} shall appear at most once
131+
withina single \grammarterm{class-property-specifier-seq}.
132+
Whenever a \grammarterm{class-key} is followed
133+
by a \grammarterm{class-head-name},
134+
the identifier \tcode{final}, \tcode{trivially_relocatable_if_eligible},
135+
or \tcode{replaceable_if_eligible}, and a colon or left brace,
136+
the identifier is interpreted as a \grammarterm{class-property-specifier}.
129137
\begin{example}
130138
\begin{codeblock}
131139
struct A;
@@ -134,12 +142,19 @@
134142

135143
struct X {
136144
struct C { constexpr operator int() { return 5; } };
137-
struct B final : C{}; // OK, definition of nested class \tcode{B},
138-
// not declaration of a bit-field member \tcode{final}
145+
struct B trivially_relocatable_if_eligible : C{};
146+
// OK, definition of nested class \tcode{B},
147+
// not declaration of a bit-field member
148+
// \tcode{trivially_relocatable_if_eligible}
139149
};
140150
\end{codeblock}
141151
\end{example}
142152

153+
\pnum
154+
If a class is marked with the \grammarterm{class-property-specifier}
155+
\tcode{final} and that class appears as a \grammarterm{class-or-decltype}
156+
in a \grammarterm{base-clause}\iref{class.derived}, the program is ill-formed.
157+
143158
\pnum
144159
\begin{note}
145160
Complete objects of class type have nonzero size.
@@ -172,6 +187,87 @@
172187
\item that has a trivial, non-deleted destructor\iref{class.dtor}.
173188
\end{itemize}
174189

190+
\pnum
191+
A class \tcode{C} is \defn{default-movable} if
192+
193+
\begin{itemize}
194+
\item overload resolution for direct-initializing an object of type \tcode{C}
195+
from an xvalue of type \tcode{C} selects a constructor that is a direct member
196+
of \tcode{C} and is neither user-provided nor deleted,
197+
198+
\item overload resolution for assigning to an lvalue of type \tcode{C} from an
199+
xvalue of type \tcode{C} selects an assignment operator function that is a
200+
direct member of \tcode{C} and is neither user-provided nor deleted, and
201+
202+
\item \tcode{C} has a destructor that is neither user-provided nor deleted.
203+
\end{itemize}
204+
205+
\pnum
206+
A class is \defn{eligible for trivial relocation} unless it
207+
\begin{itemize}
208+
\item has any virtual base classes,
209+
\item has a base class that is not a trivially relocatable class,
210+
\item has a non-static data member of an object type that is not of a
211+
trivially relocatable type, or
212+
213+
\item has a deleted destructor,
214+
\end{itemize}
215+
except that it is \impldef{whether an otherwise-eligible union having one or
216+
more subobjects of polymorphic class type is eligible for trivial relocation}
217+
whether an otherwise-eligible union having one or more subobjects of
218+
polymorphic class type is eligible for trivial relocation.
219+
220+
\pnum
221+
A class \tcode{C} is a \defnadj{trivially relocatable}{class}
222+
if it is eligible for trivial relocation and
223+
\begin{itemize}
224+
\item has the \tcode{trivially_relocatable_if_eligible} \grammarterm{class-property-specifier},
225+
\item is a union with no user-declared special member functions, or
226+
\item is default-movable.
227+
\end{itemize}
228+
229+
\pnum
230+
\begin{note}
231+
A class with const-qualified or reference non-static data members can be
232+
trivially relocatable.
233+
\end{note}
234+
235+
\pnum
236+
A class \tcode{C} is \defn{eligible for replacement} unless
237+
\begin{itemize}
238+
\item it has a base class that is not a replaceable class,
239+
\item it has a non-static data member that is not of a replaceable type,
240+
\item overload resolution fails or selects a deleted constructor when
241+
direct-initializing an object of type \tcode{C} from an xvalue of type
242+
\tcode{C}\iref{dcl.init.general},
243+
244+
\item overload resolution fails or selects a deleted assignment operator
245+
function when assigning to an lvalue of type \tcode{C} from an xvalue of type
246+
\tcode{C} \iref{expr.assign,over.assign}), or
247+
248+
\item it has a deleted destructor.
249+
\end{itemize}
250+
251+
\pnum
252+
A class \tcode{C} is a \defnadj{replaceable}{class} if it is
253+
eligible for replacement and
254+
\begin{itemize}
255+
\item has the \tcode{replaceable_if_eligible} \grammarterm{class-property-specifier},
256+
\item is a union with no user-declared special member functions, or
257+
\item is default-movable.
258+
\end{itemize}
259+
260+
\pnum
261+
\begin{note}
262+
Accessibility of the special member functions is not considered when
263+
establishing trivial relocatability or replaceability.
264+
\end{note}
265+
266+
\pnum
267+
\begin{note}
268+
Not all trivially copyable classes are trivially relocatable or replaceable.
269+
\end{note}
270+
175271
\pnum
176272
A class \tcode{S} is a \defnadj{standard-layout}{class} if it:
177273
\begin{itemize}

source/compatibility.tex

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@
9191

9292
\rSec2[diff.cpp23.dcl.dcl]{\ref{dcl}: declarations}
9393

94+
\diffref{dcl.decl.general}
95+
\change
96+
Introduction of \tcode{trivially_relocatable_if_eligible} and
97+
\tcode{replaceable_if_eligible} as identifiers with special meaning\iref{lex.name}.
98+
\rationale
99+
Support declaration of trivially relocatable and replaceable types\iref{class.prop}.
100+
\effect
101+
Valid \CppXXIII{} code can become ill-formed.
102+
\begin{example}
103+
\begin{codeblock}
104+
struct C {};
105+
struct C replaceable_if_eligible {}; // was well-formed (new variable \tcode{replaceable_if_eligible})
106+
// now ill-formed (redefines \tcode{C})
107+
\end{codeblock}
108+
\end{example}
109+
94110
\diffref{dcl.init.list}
95111
\change
96112
Pointer comparisons between \tcode{initializer_list} objects' backing arrays
@@ -212,6 +228,17 @@
212228
Valid \CppXXIII{} code that \tcode{\#include}{s} headers with these names may be
213229
invalid in this revision of \Cpp{}.
214230

231+
\diffref{res.on.macro.definitions}
232+
\change
233+
Additional restrictions on macro names.
234+
\rationale
235+
Avoid hard to diagnose or non-portable constructs.
236+
\effect
237+
Names of special identifiers may not be used as macro names.
238+
Valid \CppXXIII{} code that defines \tcode{replaceable_if_eligible} or
239+
\tcode{trivially_relocatable_if_eligible} as macros is invalid
240+
in this revision of \Cpp{}.
241+
215242
\rSec2[diff.cpp23.strings]{\ref{strings}: strings library}
216243

217244
\diffref{string.conversions}

source/expressions.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,9 +2157,9 @@
21572157
other than by changing:
21582158
\begin{itemize}
21592159
\item the size and/or alignment of the closure type,
2160-
2161-
\item whether the closure type is trivially copyable\iref{class.prop}, or
2162-
2160+
\item whether the closure type is trivially copyable\iref{class.prop},
2161+
\item whether the closure type is trivially relocatable\iref{class.prop},
2162+
\item whether the closure type is replaceable\iref{class.prop}, or
21632163
\item whether the closure type is a standard-layout class\iref{class.prop}.
21642164
\end{itemize}
21652165

source/lex.tex

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@
907907
\indextext{\idxcode{final}}%
908908
\indextext{\idxcode{module}}%
909909
\indextext{\idxcode{override}}%
910+
\indextext{\idxcode{replaceable_if_eligible}}%
911+
\indextext{\idxcode{trivially_relocatable_if_eligible}}%
910912
The identifiers in \tref{lex.name.special} have a special meaning when
911913
appearing in a certain context. When referred to in the grammar, these identifiers
912914
are used explicitly rather than using the \grammarterm{identifier} grammar production.
@@ -915,18 +917,15 @@
915917
token as a regular \grammarterm{identifier}.
916918

917919
\begin{multicolfloattable}{Identifiers with special meaning}{lex.name.special}
918-
{llllll}
919-
\keyword{final} \\
920-
\columnbreak
921-
\keyword{import} \\
922-
\columnbreak
923-
\keyword{module} \\
924-
\columnbreak
925-
\keyword{override} \\
926-
\columnbreak
927-
\keyword{post} \\
928-
\columnbreak
929-
\keyword{pre} \\
920+
{llll}
921+
\keyword{final} \\
922+
\keyword{override} \\\columnbreak
923+
\keyword{import} \\
924+
\keyword{module} \\\columnbreak
925+
\keyword{post} \\
926+
\keyword{pre} \\\columnbreak
927+
\keyword{replaceable_if_eligible} \\
928+
\keyword{trivially_relocatable_if_eligible} \\
930929
\end{multicolfloattable}
931930

932931
\pnum

source/lib-intro.tex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,6 +3785,28 @@
37853785
side effects.
37863786
\end{note}
37873787

3788+
\rSec3[library.class.props]{Properties of library classes}
3789+
3790+
\pnum
3791+
Unless explicitly stated otherwise, it is unspecified whether any class
3792+
described in \ref{\firstlibchapter} through \ref{\lastlibchapter} and
3793+
\ref{depr} is a trivially copyable class, a standard-layout class, or an
3794+
implicit-lifetime class\iref{class.prop}.
3795+
3796+
\pnum
3797+
Unless explicitly stated otherwise, it is unspecified whether any class for
3798+
which trivial relocation (i.e., the effects of
3799+
\tcode{trivially_relocate}\iref{obj.lifetime}) would be semantically equivalent
3800+
to move-construction of the destination object followed by destruction of the
3801+
source object is a trivially relocatable class\iref{class.prop}.
3802+
3803+
\pnum
3804+
Unless explicitly stated otherwise, it is unspecified whether a class \tcode{C}
3805+
is a replaceable class\iref{class.prop} if assigning an xvalue \tcode{a} of
3806+
type \tcode{C} to an object \tcode{b} of type \tcode{C} is semantically
3807+
equivalent to destroying \tcode{b} and then constructing from \tcode{a} in
3808+
\tcode{b}'s place.
3809+
37883810
\rSec3[protection.within.classes]{Protection within classes}
37893811

37903812
\pnum

0 commit comments

Comments
 (0)