Skip to content

Commit b52cea1

Browse files
authored
Merge 2019-07 CWG Motion 25
P0784R7 More constexpr containers Fixes #3004.
2 parents f7a9b2b + 209dfd5 commit b52cea1

File tree

7 files changed

+212
-94
lines changed

7 files changed

+212
-94
lines changed

source/basic.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4540,7 +4540,7 @@
45404540
\item a possibly cv-qualified class type\iref{class} that
45414541
has all of the following properties:
45424542
\begin{itemize}
4543-
\item it has a trivial destructor,
4543+
\item it has a constexpr destructor\iref{dcl.constexpr},
45444544
\item it is either a closure type\iref{expr.prim.lambda.closure},
45454545
an aggregate type\iref{dcl.init.aggr}, or
45464546
has at least one constexpr constructor or constructor template

source/classes.tex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,12 @@
21442144
A prospective destructor shall take no arguments\iref{dcl.fct}.
21452145
Each \grammarterm{decl-specifier} of the \grammarterm{decl-specifier-seq}
21462146
of a prospective destructor declaration (if any)
2147-
shall be \tcode{friend}, \tcode{inline}, or \tcode{virtual}.
2147+
shall be
2148+
\tcode{friend},
2149+
\tcode{inline},
2150+
\tcode{virtual},
2151+
\tcode{constexpr}, or
2152+
\tcode{consteval}.
21482153

21492154
\pnum
21502155
\indextext{generated destructor|see{destructor, default}}%
@@ -2234,6 +2239,13 @@
22342239
Otherwise, the destructor is
22352240
\defnx{non-trivial}{destructor!non-trivial}.
22362241

2242+
\pnum
2243+
The defaulted destructor is a constexpr destructor
2244+
if it satisfies the requirements for a constexpr destructor\iref{dcl.constexpr}.
2245+
\begin{note}
2246+
In particular, a trivial destructor is a constexpr destructor.
2247+
\end{note}
2248+
22372249
\pnum
22382250
A destructor
22392251
that is defaulted and not defined as deleted

source/declarations.tex

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -814,12 +814,9 @@
814814

815815
\pnum
816816
A \tcode{constexpr} or \tcode{consteval} specifier
817-
used in the declaration of a function that is not a constructor
817+
used in the declaration of a function
818818
declares that function to be
819819
a \defnx{constexpr function}{specifier!\idxcode{constexpr}!function}.
820-
Similarly, a \tcode{constexpr} or \tcode{consteval} specifier used in
821-
a constructor declaration declares that constructor to be
822-
a \defnx{constexpr constructor}{specifier!\idxcode{constexpr}!constructor}.
823820
A function or constructor declared with the \tcode{consteval} specifier
824821
is called an \defn{immediate function}.
825822
A destructor, an allocation function, or a deallocation function
@@ -833,14 +830,18 @@
833830

834831
\begin{itemize}
835832
\item
836-
its return type shall be a literal type;
833+
its return type (if any) shall be a literal type;
837834

838835
\item
839836
each of its parameter types shall be a literal type;
840837

841838
\item
842839
it shall not be a coroutine\iref{dcl.fct.def.coroutine};
843840

841+
\item
842+
if the function is a constructor or destructor,
843+
its class shall not have any virtual base classes;
844+
844845
\item
845846
its \grammarterm{function-body} shall not enclose\iref{stmt.stmt}
846847
\begin{itemize}
@@ -887,24 +888,10 @@
887888

888889
\pnum
889890
\indextext{specifier!\idxcode{constexpr}!constructor}%
890-
The definition of a constexpr constructor shall satisfy the
891-
following requirements:
892-
\begin{itemize}
893-
\item
894-
the class shall not have any virtual base classes;
895-
896-
\item
897-
each of the parameter types shall be a literal type.
898-
\end{itemize}
899-
900-
In addition, either its \grammarterm{function-body} shall be
901-
\tcode{= delete}, or it shall satisfy the following requirements:
891+
The definition of a constexpr constructor
892+
whose \grammarterm{function-body} is not \tcode{= delete}
893+
shall additionally satisfy the following requirements:
902894
\begin{itemize}
903-
\item
904-
either its \grammarterm{function-body} shall be \tcode{= default}, or the \grammarterm{compound-statement} of its \grammarterm{function-body}
905-
shall satisfy the requirements for a \grammarterm{function-body} of a
906-
constexpr function;
907-
908895
\item
909896
if the class is a union having variant members\iref{class.union}, exactly one of them
910897
shall be initialized;
@@ -932,6 +919,17 @@
932919
\end{codeblock}
933920
\end{example}
934921

922+
\pnum
923+
The definition of a constexpr destructor
924+
whose \grammarterm{function-body} is not \tcode{= delete}
925+
shall additionally satisfy the following requirement:
926+
\begin{itemize}
927+
\item
928+
for every subobject of class type or
929+
(possibly multi-dimensional) array thereof,
930+
that class type shall have a constexpr destructor.
931+
\end{itemize}
932+
935933
\pnum
936934
For a constexpr function or constexpr constructor
937935
that is neither defaulted nor a template,
@@ -966,12 +964,12 @@
966964
template
967965
or member function of a class template
968966
would fail to satisfy the requirements for a constexpr
969-
function or constexpr constructor,
970-
that specialization is still a constexpr function or constexpr
971-
constructor, even though a call to such a function cannot appear in a constant
967+
function,
968+
that specialization is still a constexpr function,
969+
even though a call to such a function cannot appear in a constant
972970
expression. If no specialization of the template would satisfy the
973-
requirements for a constexpr function or constexpr constructor
974-
when considered as a non-template function or constructor, the template is
971+
requirements for a constexpr function
972+
when considered as a non-template function, the template is
975973
ill-formed, no diagnostic required.
976974

977975
\pnum
@@ -987,7 +985,7 @@
987985

988986
\pnum
989987
The \tcode{constexpr} and \tcode{consteval} specifiers have no
990-
effect on the type of a constexpr function or a constexpr constructor.
988+
effect on the type of a constexpr function.
991989
\begin{example}
992990
\begin{codeblock}
993991
constexpr int bar(int x, int y) // OK
@@ -1007,6 +1005,7 @@
10071005
In any \tcode{constexpr} variable declaration,
10081006
the full-expression of the initialization
10091007
shall be a constant expression\iref{expr.const}.
1008+
A \tcode{constexpr} variable shall have constant destruction.
10101009
\begin{example}
10111010
\begin{codeblock}
10121011
struct pixel {

source/expressions.tex

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,7 +4894,19 @@
48944894
An implementation is allowed to omit a call to a replaceable global allocation
48954895
function~(\ref{new.delete.single}, \ref{new.delete.array}). When it does so,
48964896
the storage is instead provided by the implementation or provided by extending
4897-
the allocation of another \grammarterm{new-expression}. The implementation may
4897+
the allocation of another \grammarterm{new-expression}.
4898+
4899+
\pnum
4900+
During an evaluation of a constant expression,
4901+
a call to an allocation function is always omitted.
4902+
\begin{note}
4903+
Only \grammarterm{new-expression}{s} that would otherwise result in
4904+
a call to a replaceable global allocation function
4905+
can be evaluated in constant expressions (see \ref{expr.const}).
4906+
\end{note}
4907+
4908+
\pnum
4909+
The implementation may
48984910
extend the allocation of a \grammarterm{new-expression} \tcode{e1} to provide
48994911
storage for a \grammarterm{new-expression} \tcode{e2} if the
49004912
following would be true were the allocation not extended:
@@ -6864,26 +6876,22 @@
68646876
\begin{itemize}
68656877
\item
68666878
\tcode{this}\iref{expr.prim.this}, except in a constexpr
6867-
function or a constexpr constructor that is being evaluated as part
6879+
function\iref{dcl.constexpr} that is being evaluated as part
68686880
of \tcode{e};
68696881

68706882
\item
6871-
an invocation of a function other than
6872-
a constexpr constructor for a literal class,
6873-
a constexpr function,
6874-
or an implicit invocation of a trivial destructor\iref{class.dtor}
6883+
an invocation of a non-constexpr function
68756884
\begin{note} Overload resolution\iref{over.match}
6876-
is applied as usual \end{note};
6885+
is applied as usual.
6886+
\end{note};
68776887

68786888
\item
6879-
an invocation of an undefined constexpr function or an
6880-
undefined constexpr constructor;
6889+
an invocation of an undefined constexpr function;
68816890

68826891
\item
6883-
an invocation of an instantiated constexpr function or
6884-
constexpr constructor that fails to satisfy the requirements
6885-
for a constexpr function or
6886-
constexpr constructor\iref{dcl.constexpr};
6892+
an invocation of an instantiated constexpr function
6893+
that fails to satisfy the requirements
6894+
for a constexpr function;
68876895

68886896
\item
68896897
an invocation of a virtual function\iref{class.virtual}
@@ -6991,9 +6999,6 @@
69916999
\item
69927000
a \tcode{reinterpret_cast}\iref{expr.reinterpret.cast};
69937001

6994-
\item
6995-
a pseudo-destructor call\iref{expr.prim.id.dtor};
6996-
69977002
\item
69987003
modification of an object~(\ref{expr.ass}, \ref{expr.post.incr},
69997004
\ref{expr.pre.incr})
@@ -7002,10 +7007,27 @@
70027007
whose lifetime began within the evaluation of \tcode{e};
70037008

70047009
\item
7005-
a \grammarterm{new-expression}\iref{expr.new};
7010+
a \grammarterm{new-expression}\iref{expr.new},
7011+
unless the selected allocation function is
7012+
a replaceable global allocation function~(\ref{new.delete.single},
7013+
\ref{new.delete.array}) and
7014+
the allocated storage is deallocated within the evaluation of \tcode{e};
7015+
7016+
\item
7017+
a \grammarterm{delete-expression}\iref{expr.delete},
7018+
unless it deallocates a region of storage
7019+
allocated within the evaluation of \tcode{e};
7020+
7021+
\item
7022+
a call to an instance of
7023+
\tcode{std::allocator<T>::allocate}\iref{allocator.members},
7024+
unless the allocated storage is deallocated within the evaluation of \tcode{e};
70067025

70077026
\item
7008-
a \grammarterm{delete-expression}\iref{expr.delete};
7027+
a call to an instance of
7028+
\tcode{std::allocator<T>::deallocate}\iref{allocator.members},
7029+
unless it deallocates a region of storage
7030+
allocated within the evaluation of \tcode{e};
70097031

70107032
\item
70117033
an \grammarterm{await-expression}\iref{expr.await};
@@ -7076,6 +7098,50 @@
70767098
\end{codeblock}
70777099
\end{example}
70787100

7101+
\pnum
7102+
For the purposes of determining
7103+
whether an expression is a core constant expression,
7104+
the evaluation of a call to a member function of \tcode{std::allocator<T>}
7105+
as defined in \ref{allocator.members}, where \tcode{T} is a literal type,
7106+
does not disqualify the expression from being a core constant expression,
7107+
even if the actual evaluation of such a call
7108+
would otherwise fail the requirements for a core constant expression.
7109+
Similarly, the evaluation of a call to
7110+
\tcode{std::destroy_at},
7111+
\tcode{std::ranges::destroy_at},
7112+
\tcode{std::construct_at}, or
7113+
\tcode{std::ranges::construct_at}
7114+
is a valid core constant expression unless:
7115+
\begin{itemize}
7116+
\item
7117+
for a call to \tcode{std::construct_at} or \tcode{std::ranges::construct_at},
7118+
the first argument, of type \tcode{T*},
7119+
does not point to storage allocated with \tcode{std::allocator<T>} or
7120+
the evaluation of the underlying constructor call
7121+
is not a core constant expression, or
7122+
\item
7123+
for a call to \tcode{std::destroy_at} or \tcode{std::ranges::destroy_at},
7124+
the first argument, of type \tcode{T*},
7125+
does not point to storage allocated with \tcode{std::allocator<T>} or
7126+
the evaluation of the underlying destructor call
7127+
is not a core constant expression.
7128+
\end{itemize}
7129+
7130+
\pnum
7131+
An object \tcode{a} is said to have \defnadj{constant}{destruction} if:
7132+
\begin{itemize}
7133+
\item
7134+
it is not of class type nor (possibly multi-dimensional) array thereof, or
7135+
\item
7136+
it is of class type or (possibly multi-dimensional) array thereof,
7137+
that class type has a constexpr destructor, and
7138+
for a hypothetical expression \tcode{e}
7139+
whose only effect is to destroy \tcode{a},
7140+
\tcode{e} would be a core constant expression
7141+
if the lifetime of \tcode{a} and its non-mutable subobjects
7142+
(but not its mutable subobjects) were considered to start within \tcode{e}.
7143+
\end{itemize}
7144+
70797145
\pnum
70807146
An \defnadj{integral constant}{expression}
70817147
is an expression of integral or

source/preprocessor.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,7 @@
16691669
\defnxname{cpp_char8_t} & \tcode{201811L} \\ \rowsep
16701670
\defnxname{cpp_conditional_explicit} & \tcode{201806L} \\ \rowsep
16711671
\defnxname{cpp_constexpr} & \tcode{201907L} \\ \rowsep
1672+
\defnxname{cpp_constexpr_dynamic_alloc} & \tcode{201907L} \\ \rowsep
16721673
\defnxname{cpp_constinit} & \tcode{201907L} \\ \rowsep
16731674
\defnxname{cpp_coroutines} & \tcode{201902L} \\ \rowsep
16741675
\defnxname{cpp_decltype} & \tcode{200707L} \\ \rowsep

source/support.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@
574574
\tcode{<string_view>} \tcode{<tuple>} \tcode{<utility>} \\ \rowsep
575575
\defnlibxname{cpp_lib_constexpr_swap_algorithms} & \tcode{201806L} &
576576
\tcode{<algorithm>} \\ \rowsep
577+
\defnlibxname{cpp_lib_constexpr_dynamic_alloc} & \tcode{201907L} &
578+
\tcode{<memory>} \\ \rowsep
577579
\defnlibxname{cpp_lib_destroying_delete} & \tcode{201806L} &
578580
\tcode{<new>} \\ \rowsep
579581
\defnlibxname{cpp_lib_enable_shared_from_this} & \tcode{201603L} &

0 commit comments

Comments
 (0)