Difference between revisions of "cpp/concepts"
m (→See also: +"Named Requirements") |
(Move #Equality preservation section (with some mods) here, then remove its content (replicas) from other pages (leaving mostly a link), per talk.) |
||
Line 8: | Line 8: | ||
In general, only the syntactic requirements can be checked by the compiler. If the validity or meaning of a program depends whether a sequence of template arguments models a concept, and the concept is satisfied but not modeled, or if a semantic requirement is not met at the point of use, the program is ill-formed, [[cpp/language/ndr|no diagnostic required]]. | In general, only the syntactic requirements can be checked by the compiler. If the validity or meaning of a program depends whether a sequence of template arguments models a concept, and the concept is satisfied but not modeled, or if a semantic requirement is not met at the point of use, the program is ill-formed, [[cpp/language/ndr|no diagnostic required]]. | ||
+ | ===Equality preservation=== | ||
+ | An expression is ''equality preserving'' if it results in equal outputs given equal inputs. | ||
+ | * The inputs to an expression consist of its operands. | ||
+ | * The outputs of an expression consist of its result and all operands modified by the expression (if any). | ||
+ | |||
+ | In specification of standard concepts, operands are defined as the largest subexpressions that include only: | ||
+ | * an [[cpp/language/expressions#Primary expressions|id-expression]], and | ||
+ | * invocations of {{ltt|cpp/utility/move|std::move}}, {{lc|std::forward}}, and {{lc|std::declval}}. | ||
+ | |||
+ | The cv-qualification and value category of each operand is determined by assuming that each template type parameter denotes a cv-unqualified complete non-array object type. | ||
+ | |||
+ | Every expression required to be ''equality preserving'' is further required to be ''stable'': two evaluations of such an expression with the same input objects must have equal outputs absent any explicit intervening modification of those input objects. | ||
+ | |||
+ | Unless noted otherwise, every expression used in a [[cpp/language/requires|requires-expression]] of the [[#Standard library concepts (since C++20)|standard library concepts]] is required to be ''equality preserving'' and ''stable'', and the evaluation of the expression may modify only its non-constant operands. Operands that are constant must not be modified. | ||
+ | |||
+ | ===Standard library concepts {{mark since c++20}}=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc namespace | std }} | + | {{dsc namespace|std}} |
− | {{dsc h2 | Core language concepts}} | + | {{dsc h2|Core language concepts}} |
− | {{dsc header | concepts}} | + | {{dsc header|concepts}} |
− | {{dsc inc | cpp/concepts/dsc same_as}} | + | {{dsc inc|cpp/concepts/dsc same_as}} |
− | {{dsc inc | cpp/concepts/dsc derived_from}} | + | {{dsc inc|cpp/concepts/dsc derived_from}} |
− | {{dsc inc | cpp/concepts/dsc convertible_to}} | + | {{dsc inc|cpp/concepts/dsc convertible_to}} |
− | {{dsc inc | cpp/concepts/dsc common_reference_with}} | + | {{dsc inc|cpp/concepts/dsc common_reference_with}} |
− | {{dsc inc | cpp/concepts/dsc common_with}} | + | {{dsc inc|cpp/concepts/dsc common_with}} |
− | {{dsc inc | cpp/concepts/dsc integral}} | + | {{dsc inc|cpp/concepts/dsc integral}} |
− | {{dsc inc | cpp/concepts/dsc signed_integral}} | + | {{dsc inc|cpp/concepts/dsc signed_integral}} |
− | {{dsc inc | cpp/concepts/dsc unsigned_integral}} | + | {{dsc inc|cpp/concepts/dsc unsigned_integral}} |
− | {{dsc inc | cpp/concepts/dsc floating_point}} | + | {{dsc inc|cpp/concepts/dsc floating_point}} |
− | {{dsc inc | cpp/concepts/dsc assignable_from}} | + | {{dsc inc|cpp/concepts/dsc assignable_from}} |
− | {{dsc inc | cpp/concepts/dsc swappable}} | + | {{dsc inc|cpp/concepts/dsc swappable}} |
− | {{dsc inc | cpp/concepts/dsc destructible}} | + | {{dsc inc|cpp/concepts/dsc destructible}} |
− | {{dsc inc | cpp/concepts/dsc constructible_from}} | + | {{dsc inc|cpp/concepts/dsc constructible_from}} |
− | {{dsc inc | cpp/concepts/dsc default_initializable}} | + | {{dsc inc|cpp/concepts/dsc default_initializable}} |
− | {{dsc inc | cpp/concepts/dsc move_constructible}} | + | {{dsc inc|cpp/concepts/dsc move_constructible}} |
− | {{dsc inc | cpp/concepts/dsc copy_constructible}} | + | {{dsc inc|cpp/concepts/dsc copy_constructible}} |
− | {{dsc h2 | Comparison concepts}} | + | {{dsc h2|Comparison concepts}} |
− | {{dsc header | concepts}} | + | {{dsc header|concepts}} |
− | {{dsc inc | cpp/concepts/dsc boolean-testable}} | + | {{dsc inc|cpp/concepts/dsc boolean-testable}} |
− | {{dsc inc | cpp/concepts/dsc equality_comparable}} | + | {{dsc inc|cpp/concepts/dsc equality_comparable}} |
− | {{dsc inc | cpp/concepts/dsc totally_ordered}} | + | {{dsc inc|cpp/concepts/dsc totally_ordered}} |
− | {{dsc header | compare}} | + | {{dsc header|compare}} |
− | {{dsc inc | cpp/utility/compare/dsc three_way_comparable}} | + | {{dsc inc|cpp/utility/compare/dsc three_way_comparable}} |
− | {{dsc h2 | Object concepts}} | + | {{dsc h2|Object concepts}} |
− | {{dsc header | concepts}} | + | {{dsc header|concepts}} |
− | {{dsc inc | cpp/concepts/dsc movable}} | + | {{dsc inc|cpp/concepts/dsc movable}} |
− | {{dsc inc | cpp/concepts/dsc copyable}} | + | {{dsc inc|cpp/concepts/dsc copyable}} |
− | {{dsc inc | cpp/concepts/dsc semiregular}} | + | {{dsc inc|cpp/concepts/dsc semiregular}} |
− | {{dsc inc | cpp/concepts/dsc regular}} | + | {{dsc inc|cpp/concepts/dsc regular}} |
− | {{dsc h2 | Callable concepts}} | + | {{dsc h2|Callable concepts}} |
− | {{dsc header | concepts}} | + | {{dsc header|concepts}} |
− | {{dsc inc | cpp/concepts/dsc invocable}} | + | {{dsc inc|cpp/concepts/dsc invocable}} |
− | {{dsc inc | cpp/concepts/dsc predicate}} | + | {{dsc inc|cpp/concepts/dsc predicate}} |
− | {{dsc inc | cpp/concepts/dsc relation}} | + | {{dsc inc|cpp/concepts/dsc relation}} |
− | {{dsc inc | cpp/concepts/dsc equivalence_relation}} | + | {{dsc inc|cpp/concepts/dsc equivalence_relation}} |
− | {{dsc inc | cpp/concepts/dsc strict_weak_order}} | + | {{dsc inc|cpp/concepts/dsc strict_weak_order}} |
{{dsc end}} | {{dsc end}} | ||
Revision as of 13:33, 23 February 2023
The concepts library provides definitions of fundamental library concepts that can be used to perform compile-time validation of template arguments and perform function dispatch based on properties of types. These concepts provide a foundation for equational reasoning in programs.
Most concepts in the standard library impose both syntactic and semantic requirements. It is said that a standard concept is satisfied if its syntactic requirements are met, and is modeled if it is satisfied and its semantic requirements (if any) are also met.
In general, only the syntactic requirements can be checked by the compiler. If the validity or meaning of a program depends whether a sequence of template arguments models a concept, and the concept is satisfied but not modeled, or if a semantic requirement is not met at the point of use, the program is ill-formed, no diagnostic required.
Contents |
Equality preservation
An expression is equality preserving if it results in equal outputs given equal inputs.
- The inputs to an expression consist of its operands.
- The outputs of an expression consist of its result and all operands modified by the expression (if any).
In specification of standard concepts, operands are defined as the largest subexpressions that include only:
- an id-expression, and
- invocations of std::move, std::forward, and std::declval.
The cv-qualification and value category of each operand is determined by assuming that each template type parameter denotes a cv-unqualified complete non-array object type.
Every expression required to be equality preserving is further required to be stable: two evaluations of such an expression with the same input objects must have equal outputs absent any explicit intervening modification of those input objects.
Unless noted otherwise, every expression used in a requires-expression of the standard library concepts is required to be equality preserving and stable, and the evaluation of the expression may modify only its non-constant operands. Operands that are constant must not be modified.
Standard library concepts (since C++20)
Defined in namespace
std | |
Core language concepts | |
Defined in header
<concepts> | |
(C++20) |
specifies that a type is the same as another type (concept) |
(C++20) |
specifies that a type is derived from another type (concept) |
(C++20) |
specifies that a type is implicitly convertible to another type (concept) |
(C++20) |
specifies that two types share a common reference type (concept) |
(C++20) |
specifies that two types share a common type (concept) |
(C++20) |
specifies that a type is an integral type (concept) |
(C++20) |
specifies that a type is an integral type that is signed (concept) |
(C++20) |
specifies that a type is an integral type that is unsigned (concept) |
(C++20) |
specifies that a type is a floating-point type (concept) |
(C++20) |
specifies that a type is assignable from another type (concept) |
(C++20) |
specifies that a type can be swapped or that two types can be swapped with each other (concept) |
(C++20) |
specifies that an object of the type can be destroyed (concept) |
(C++20) |
specifies that a variable of the type can be constructed from or bound to a set of argument types (concept) |
(C++20) |
specifies that an object of a type can be default constructed (concept) |
(C++20) |
specifies that an object of a type can be move constructed (concept) |
(C++20) |
specifies that an object of a type can be copy constructed and move constructed (concept) |
Comparison concepts | |
Defined in header
<concepts> | |
(C++20) |
specifies that a type can be used in Boolean contexts (exposition-only concept*) |
specifies that operator == is an equivalence relation (concept) | |
specifies that the comparison operators on the type yield a total order (concept) | |
Defined in header
<compare> | |
specifies that operator <=> produces consistent result on given types (concept) | |
Object concepts | |
Defined in header
<concepts> | |
(C++20) |
specifies that an object of a type can be moved and swapped (concept) |
(C++20) |
specifies that an object of a type can be copied, moved, and swapped (concept) |
(C++20) |
specifies that an object of a type can be copied, moved, swapped, and default constructed (concept) |
(C++20) |
specifies that a type is regular, that is, it is both semiregular and equality_comparable (concept) |
Callable concepts | |
Defined in header
<concepts> | |
(C++20) |
specifies that a callable type can be invoked with a given set of argument types (concept) |
(C++20) |
specifies that a callable type is a Boolean predicate (concept) |
(C++20) |
specifies that a callable type is a binary relation (concept) |
(C++20) |
specifies that a relation imposes an equivalence relation (concept) |
(C++20) |
specifies that a relation imposes a strict weak ordering (concept) |
Additional concepts can be found in the iterators library, the algorithms library, and the ranges library.