Files
cppdraft_translate/cppdraft/support/contract.md
2025-10-25 03:02:53 +03:00

254 lines
10 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[support.contract]
# 17 Language support library [[support]](./#support)
## 17.10 Contract-violation handling [support.contract]
### [17.10.1](#contracts.syn) Header <contracts> synopsis [[contracts.syn]](contracts.syn)
[1](#contracts.syn-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4387)
The header [<contracts>](#header:%3ccontracts%3e "17.10.1Header <contracts> synopsis[contracts.syn]") defines types
for reporting information about contract violations ([[basic.contract.eval]](basic.contract.eval "6.11.2Evaluation"))[.](#contracts.syn-1.sentence-1)
[🔗](#header:%3ccontracts%3e)
// all freestandingnamespace std::contracts {enum class assertion_kind : *unspecified* { pre = 1,
post = 2,
assert = 3}; enum class evaluation_semantic : *unspecified* { ignore = 1,
observe = 2,
enforce = 3,
quick_enforce = 4}; enum class detection_mode : *unspecified* { predicate_false = 1,
evaluation_exception = 2}; class contract_violation {// no user-accessible constructorpublic: contract_violation(const contract_violation&) = delete;
contract_violation& operator=(const contract_violation&) = delete; *see below* ~contract_violation(); const char* comment() const noexcept;
contracts::detection_mode detection_mode() const noexcept;
exception_ptr evaluation_exception() const noexcept; bool is_terminating() const noexcept;
assertion_kind kind() const noexcept;
source_location location() const noexcept;
evaluation_semantic semantic() const noexcept; }; void invoke_default_contract_violation_handler(const contract_violation&);}
### [17.10.2](#enum) Enumerations [[support.contract.enum]](support.contract.enum)
[1](#enum-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4438)
*Recommended practice*: For all enumerations in [[support.contract.enum]](#enum "17.10.2Enumerations"),
if implementation-defined enumerators are provided,
they should have a minimum value of 1000[.](#enum-1.sentence-1)
[2](#enum-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4444)
The enumerators of assertion_kind correspond to
the syntactic forms of a contract assertion ([[basic.contract.general]](basic.contract.general "6.11.1General")),
with meanings listed in Table [44](#tab:support.contract.enum.kind "Table 44: Enum assertion_­kind")[.](#enum-2.sentence-1)
Table [44](#tab:support.contract.enum.kind) — Enum assertion_kind [[tab:support.contract.enum.kind]](./tab:support.contract.enum.kind)
| [🔗](#tab:support.contract.enum.kind-row-1)<br>**Name** | **Meaning** |
| --- | --- |
| [🔗](#tab:support.contract.enum.kind-row-2)<br>pre | A precondition assertion |
| [🔗](#tab:support.contract.enum.kind-row-3)<br>post | A postcondition assertion |
| [🔗](#tab:support.contract.enum.kind-row-4)<br>assert | An [*assertion-statement*](stmt.contract.assert#nt:assertion-statement "8.9Assertion statement[stmt.contract.assert]") |
| [🔗](#tab:support.contract.enum.kind-row-5) |
[3](#enum-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4459)
The enumerators of evaluation_semantic correspond to
the evaluation semantics with which
a contract assertion may be evaluated ([[basic.contract.eval]](basic.contract.eval "6.11.2Evaluation")),
with meanings listed in Table [45](#tab:support.contract.enum.semantic "Table 45: Enum evaluation_­semantic")[.](#enum-3.sentence-1)
Table [45](#tab:support.contract.enum.semantic) — Enum evaluation_semantic [[tab:support.contract.enum.semantic]](./tab:support.contract.enum.semantic)
| [🔗](#tab:support.contract.enum.semantic-row-1)<br>**Name** | **Meaning** |
| --- | --- |
| [🔗](#tab:support.contract.enum.semantic-row-2)<br>ignore | Ignore evaluation semantic |
| [🔗](#tab:support.contract.enum.semantic-row-3)<br>observe | Observe evaluation semantic |
| [🔗](#tab:support.contract.enum.semantic-row-4)<br>enforce | Enforce evaluation semantic |
| [🔗](#tab:support.contract.enum.semantic-row-5)<br>quick_enforce | Quick-enforce evaluation semantic |
| [🔗](#tab:support.contract.enum.semantic-row-6) |
[4](#enum-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4476)
The enumerators of detection_mode correspond to the manners in which a
contract violation can be identified ([[basic.contract.eval]](basic.contract.eval "6.11.2Evaluation")), with
meanings listed in Table [46](#tab:support.contract.enum.detection "Table 46: Enum detection_­mode")[.](#enum-4.sentence-1)
Table [46](#tab:support.contract.enum.detection) — Enum detection_mode [[tab:support.contract.enum.detection]](./tab:support.contract.enum.detection)
| [🔗](#tab:support.contract.enum.detection-row-1)<br>**Name** | **Meaning** |
| --- | --- |
| [🔗](#tab:support.contract.enum.detection-row-2)<br>predicate_false | The predicate of the contract assertion evaluated to false or would have evaluated to false[.](#tab:support.contract.enum.detection-row-2-column-2-sentence-1) |
| [🔗](#tab:support.contract.enum.detection-row-3)<br>evaluation_exception | An uncaught exception occurred during evaluation of the contract assertion[.](#tab:support.contract.enum.detection-row-3-column-2-sentence-1) |
| [🔗](#tab:support.contract.enum.detection-row-4) |
### [17.10.3](#violation) Class contract_violation [[support.contract.violation]](support.contract.violation)
[1](#violation-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4491)
The class contract_violation defines the type of objects used to represent
a contract violation that has been detected
during the evaluation of a contract assertion
with a particular evaluation semantic ([[basic.contract.eval]](basic.contract.eval "6.11.2Evaluation"))[.](#violation-1.sentence-1)
Objects of this type can
be created only by the implementation[.](#violation-1.sentence-2)
It isimplementation-defined
whether the destructor is virtual[.](#violation-1.sentence-3)
[🔗](#violation-itemdecl:1)
`const char* comment() const noexcept;
`
[2](#violation-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4509)
*Returns*: Animplementation-definedntmbs in
the ordinary literal encoding ([[lex.charset]](lex.charset "5.3.1Character sets"))[.](#violation-2.sentence-1)
[3](#violation-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4516)
*Recommended practice*: The string returned
should contain a textual representation
of the predicate of the violated contract assertion
or an empty string if
storing a textual representation is undesired[.](#violation-3.sentence-1)
[*Note [1](#violation-note-1)*:
The string can represent a
truncated, reformatted, or summarized rendering of the
predicate, before or after preprocessing[.](#violation-3.sentence-2)
— *end note*]
[🔗](#violation-itemdecl:2)
`contracts::detection_mode detection_mode() const noexcept;
`
[4](#violation-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4536)
*Returns*: The enumerator value
corresponding to
the manner in which the contract violation was identified[.](#violation-4.sentence-1)
[🔗](#violation-itemdecl:3)
`exception_ptr evaluation_exception() const noexcept;
`
[5](#violation-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4550)
*Returns*: If the contract violation occurred
because the evaluation of the predicate exited via an exception,
an exception_ptr object that refers to
that exception or a copy of that exception;
otherwise, a null exception_ptr object[.](#violation-5.sentence-1)
[🔗](#violation-itemdecl:4)
`bool is_terminating() const noexcept;
`
[6](#violation-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4566)
*Returns*: true if the evaluation semantic is
a terminating semantic ([[basic.contract.eval]](basic.contract.eval "6.11.2Evaluation"));
otherwise, false[.](#violation-6.sentence-1)
[🔗](#violation-itemdecl:5)
`assertion_kind kind() const noexcept;
`
[7](#violation-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4579)
*Returns*: The enumerator value
corresponding to
the syntactic form of the violated contract assertion[.](#violation-7.sentence-1)
[🔗](#violation-itemdecl:6)
`source_location location() const noexcept;
`
[8](#violation-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4592)
*Returns*: A source_location object
withimplementation-defined
value[.](#violation-8.sentence-1)
[9](#violation-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4599)
*Recommended practice*: The value returned should be
a default constructed source_location object
or a value identifying the violated contract assertion:
- [(9.1)](#violation-9.1)
When possible,
if the violated contract assertion was a precondition,
the source location of the function invocation should be returned[.](#violation-9.1.sentence-1)
- [(9.2)](#violation-9.2)
Otherwise,
the source location of the contract assertion should be returned[.](#violation-9.2.sentence-1)
[🔗](#violation-itemdecl:7)
`evaluation_semantic semantic() const noexcept;
`
[10](#violation-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4622)
*Returns*: The enumerator value
corresponding to
the evaluation semantic with which
the violated contract assertion was evaluated[.](#violation-10.sentence-1)
### [17.10.4](#invoke) Invoke default handler [[support.contract.invoke]](support.contract.invoke)
[🔗](#invoke-itemdecl:1)
`void invoke_default_contract_violation_handler(const contract_violation& v);
`
[1](#invoke-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/support.tex#L4638)
*Effects*: Invokes the default contract-violation handler ([[basic.contract.handler]](basic.contract.handler "6.11.3Contract-violation handler"))
with the argument v[.](#invoke-1.sentence-1)