x
An object of type X
Valid expressions
Name |
Expression |
Return type |
Default constructor |
X() |
X |
Default constructor |
X x; [1] |
|
Expression semantics
Name |
Expression |
Default constructor |
X() |
Default constructor |
X x; |
Models
• int
• vector
Notes
[1] The form X x = X() is not guaranteed to be a valid expression, because it uses a copy constructor. A type that is DefaultConstructible is not necessarily Assignable
See also
Assignable
Category: utilities
Component type: concept
Description
A type is EqualityComparable if objects of that type can be compared for equality using operator== , and if operator== is an equivalence relation.
Notation
X
A type that is a model of EqualityComparable
x, y, z
Object of type X
Valid expressions
Name |
Expression |
Return type |
Equality |
x == y |
Convertible to bool |
Inequality |
x != y |
Convertible to bool |
Expression semantics
Name |
Expression |
Precondition |
Semantics |
Equality |
x == y |
x and y are in the domain of == |
|
Inequality |
x != y |
x and y are in the domain of == |
Equivalent to !(x == y) |
Invariants
Identity |
&x == &y implies x == y |
Reflexivity |
x == x |
Symmetry |
x == y implies y == x |
Transitivity |
x == y and y == z implies x == z |
Models
• int
• vector
See also
LessThanComparable.
Category: utilities
Component type: concept
Description
A type is LessThanComparable if it is ordered: it must be possible to compare two objects of that type using operator< , and operator< must be a partial ordering.
Notation
X
A type that is a model of LessThanComparable
x, y, z
Object of type X
Definitions
Consider the relation !(x < y) && !(y < x) . If this relation is transitive (that is, if !(x < y) && !(y < x) && !(y < z) && !(z < y) implies !(x < z) && !(z < x) ), then it satisfies the mathematical definition of an equivalence relation. In this case, operator< is a strict weak ordering .
If operator< is a strict weak ordering, and if each equivalence class has only a single element, then operator< is a total ordering .
Valid expressions
Name |
Expression |
Return type |
Less |
x < y |
Convertible to bool |
Greater |
x > y |
Convertible to bool |
Less or equal |
x <= y |
Convertible to bool |
Greater or equal |
x >= y |
Convertible to bool |
Expression semantics
Name |
Expression |
Precondition |
Semantics |
Less |
x < y |
x and y are in the domain of < |
|
Greater |
x > y |
x and y are in the domain of < |
Equivalent to y < x [1] |
Less or equal |
x <= y |
x and y are in the domain of < |
Equivalent to !(y < x) [1] |
Greater or equal |
x >= y |
x and y are in the domain of < |
Equivalent to !(x < y) [1] |
Invariants
Irreflexivity |
x < x must be false. |
Antisymmetry |
x < y implies !(y < x) [2] |
Transitivity |
x < y and y < z implies x < z [3] |
Models
• int
Notes
[1] Only operator< is fundamental; the other inequality operators are essentially syntactic sugar.
[2] Antisymmetry is a theorem, not an axiom: it follows from irreflexivity and transitivity.
[3] Because of irreflexivity and transitivity, operator< always satisfies the definition of a partial ordering . The definition of a strict weak ordering is stricter, and the definition of a total ordering is stricter still.
See also
EqualityComparable, StrictWeakOrdering
Category: utilities
Component type: function
Prototype
template
bool operator!=(const T& x, const T& y);
template
bool operator>(const T& x, const T& y);
template
bool operator<=(const T& x, const T& y);
template
bool operator>=(const T& x, const T& y);
Description
The Equality Comparable requirements specify that it must be possible to compare objects using operator!= as well as operator== ; similarly, the LessThan Comparable requirements include operator> , operator<= and operator>= as well as operator< . Logically, however, most of these operators are redundant: all of them can be defined in terms of operator== and operator< .
These four templates use operator== and operator< to define the other four relational operators. They exist purely for the sake of convenience: they make it possible to write algorithms in terms of the operators != , > , <= , and >= , without requiring that those operators be explicitly defined for every type.
As specified in the Equality Comparable requirements, x != y is equivalent to !(x == y) . As specified in the LessThan Comparable requirements, x > y is equivalent to y < x , x >= y is equivalent to !(x < y) , and x <= y is equivalent to !(y < x) .
Читать дальше