Skip to content

[2025-02 CWG Motion 5] P2900R14 - Contracts for C++ #7695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
587 changes: 548 additions & 39 deletions source/basic.tex

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions source/classes.tex
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@

\begin{bnf}
\nontermdef{member-declarator}\br
declarator \opt{virt-specifier-seq} \opt{pure-specifier}\br
declarator requires-clause\br
declarator \opt{virt-specifier-seq} \opt{function-contract-specifier-seq} \opt{pure-specifier}\br
declarator requires-clause \opt{function-contract-specifier-seq}\br
declarator brace-or-equal-initializer\br
\opt{identifier} \opt{attribute-specifier-seq} \terminal{:} constant-expression \opt{brace-or-equal-initializer}
\end{bnf}
Expand Down Expand Up @@ -527,6 +527,12 @@
the program is ill-formed; see~\ref{temp.spec.general}.
\end{note}

\pnum
The optional \grammarterm{function-contract-specifier-seq}\iref{dcl.contract.func})
in a \grammarterm{member-declarator}
shall be present only if
the \grammarterm{declarator} declares a function.

\pnum
\indextext{definition!class}%
The \grammarterm{member-specification} in a class definition declares the
Expand Down Expand Up @@ -618,7 +624,8 @@
\item function body\iref{dcl.fct.def.general},
\item default argument\iref{dcl.fct.default},
\item default template argument\iref{temp.param},
\item \grammarterm{noexcept-specifier}\iref{except.spec}, or
\item \grammarterm{noexcept-specifier}\iref{except.spec},
\item \grammarterm{function-contract-specifier}\iref{dcl.contract.func}, or
\item default member initializer
\end{itemize}
within the \grammarterm{member-specification} of the class or class template.
Expand Down Expand Up @@ -4289,17 +4296,17 @@
\begin{codeblock}
class A {
typedef int I; // private member
I f();
friend I g(I);
I f() pre(A::x > 0);
friend I g(I) post(A::x <= 0);
static I x;
template<int> struct Q;
template<int> friend struct R;
protected:
struct B { };
};

A::I A::f() { return 0; }
A::I g(A::I p = A::x);
A::I A::f() pre(A::x > 0) { return 0; }
A::I g(A::I p = A::x) post(A::x <= 0);
A::I g(A::I p) { return 0; }
A::I A::x = 0;
template<A::I> struct A::Q { };
Expand Down Expand Up @@ -5715,18 +5722,27 @@
\pnum
\indextext{initialization!member function call during}%
Member functions (including virtual member functions, \ref{class.virtual}) can be
called for an object under construction.
Similarly, an object under construction can be the operand of the
called for an object under construction or destruction.
Similarly, an object under construction or destruction can be the operand of the
\tcode{typeid}
operator\iref{expr.typeid} or of a
\keyword{dynamic_cast}\iref{expr.dynamic.cast}.
However, if these operations are performed in a
\grammarterm{ctor-initializer}
However, if these operations are performed
during evaluation of
\begin{itemize}
\item
a \grammarterm{ctor-initializer}
(or in a function called directly or indirectly from a
\grammarterm{ctor-initializer})
before all the
\grammarterm{mem-initializer}{s}
for base classes have completed, the program has undefined behavior.
for base classes have completed,
\item
a precondition assertion of a constructor, or
\item
a postcondition assertion of a destructor\iref{dcl.contract.func},
\end{itemize}
the program has undefined behavior.
\begin{example}
\begin{codeblock}
class A {
Expand Down Expand Up @@ -6049,6 +6065,9 @@
or from a destructor,
including during the construction or destruction of the class's non-static data
members,
or during the evaluation of
a postcondition assertion of a constructor or
a precondition assertion of a destructor\iref{dcl.contract.func},
and the object to which the call applies is the object (call it \tcode{x}) under construction or
destruction,
the function called is the
Expand Down
18 changes: 18 additions & 0 deletions source/compatibility.tex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
ISO \CppXXIII{},
by the chapters of this document.

\rSec2[diff.cpp23.lex]{\ref{lex}: Lexical conventions}

\diffref{lex.key}
\change
New keywords.
\rationale
Required for new features.
\begin{itemize}
\item
The \keyword{contract_assert} keyword
is added to introduce a contract assertion
through an \grammarterm{assertion-statement}\iref{stmt.contract.assert}.
\end{itemize}
\effect
Valid \CppXXIII{} code using \keyword{contract_assert} as an identifier
is not valid in this revision of \Cpp{}.

\rSec2[diff.cpp23.expr]{\ref{expr}: expressions}

\diffref{expr.arith.conv}
Expand Down Expand Up @@ -182,6 +199,7 @@
New functionality.
\effect
The following \Cpp{} headers are new:
\libheaderrefx{contracts}{support.contract},
\libheaderref{debugging},
\libheaderrefx{hazard_pointer}{hazard.pointer.syn},
\libheaderrefx{inplace_vector}{inplace.vector.syn},
Expand Down
Loading