Since c++11 the c ++ standards committee has made it a goal to approve a new standard every 3 years. This has been the case to date and we have had the following standards:
Standardization is divided into four working groups:
- Nucleus (Core): Maintenance of the language and standardization of new proposals.
- Evolution : Development of new features for the language.
- Library : Maintenance of the standard library and standardization of new proposals.
- Library evolution : Development of new features for the standard library.
In July 2019, the C++20 standard was considered closed. This implies that no new features can be added (although some could be removed if problems are detected) and approved features are being reviewed for possible improvements or minor corrections.
At this last meeting, it was considered that the Contracts 2 proposal (technical document P0542R4 ) would not be incorporated in C++20 and is delayed to C++23.
We are going to list some of the main features that C++20 will incorporate.
1 Due to some strange oversight, I wrote two threads about C++17.
2 According to Wikipedia the contract is:
Design - by-contract is a methodology for the design and implementation of applications and components popularized by the Eiffel programming language . It consists of considering design elements as participants in a relationship similar to a business contract. Thus, components can be designed assuming that certain input conditions (pre-conditions) will be met, while certain output conditions (post-conditions) must be guaranteed, as well as class invariance (properties that remain invariant despite of the processing performed by the component).
C++ Contracts will follow the attribute syntax (added in C++11):
[[contrato modificador: expresión-condicional]]
Contracts have been moved to C++23, the following is subject to change: The available contracts are:
expects
: A type contractexpects
is a pre-condition. Indicates what a function expects from its arguments, and that contract is expected to hold throughout the function call.ensures
: A type contractensures
is a post-condition. Indicates the state that the objects passed to a function (or the return from it) are expected to have at the end of the call.asert
: A type contractassert
is an assertion. Indicates a condition that is expected to be satisfied where the contract appears.
When a contract is not fulfilled, a function call will be made with the following signature:
void(const std::contract_violation &);
The object std::contract_violation
will contain information about the breach of contract such as where (file, line, function) has been breached.
Modifiers can be applied to contracts, modifiers will affect how code with contracts is generated and whether or not contracts are checked at runtime. The available modifiers are:
axiom
: The verification of the conditions of this contract is expected to have no cost, it is not evaluated at execution time.default
: Verification of the conditions of this contract is expected to be inexpensive.audit
: Verification of the terms of this contract is expected to be expensive.
The contracts that will be verified at compile time contain code that will be compiled, you can specify the level with which you want to compile each translation unit and the resulting behavior will be:
- Default: Checks (compiles) contracts marked as
default
(contracts that have no modifier are considereddefault
). - Audit: Verifies (compiles) the contracts marked as
audit
anddefault
.