52942.fb2
Category: utilities
Component type: concept
A type is Assignable if it is possible to copy objects of that type and to assign values to variables.
X
A type that is a model of Assignable
x, y
Object of type X
Name | Expression | Return type |
---|---|---|
Copy constructor | X(x) | X |
Copy constructor | X x(y); X x = y; | |
Assignment | x = y [1] | X& |
Swap | swap(x,y) | void |
Name | Expression | Semantics | Postcondition |
---|---|---|---|
Copy constructor | X(x) | X(x) is a copy of x [2] | |
Copy constructor | X(x) | X(x) is a copy of x [2] | |
Copy constructor | X x(y); X x = y; | x is a copy of y [2] | |
Assignment | x = y [1] | x is a copy of y [2] | |
Swap | swap (x,y) | Equivalent to{ X tmp = x; x = y; y = tmp; } |
• int
[1] One implication of this requirement is that a const type is not Assignable. For example, const int is not Assignable: if x is declared to be of type const int, then x = 7 is illegal. Similarly, the type pair<const int, int> is not Assignable.
[2] The reason this says "x is a copy of y ", rather than "x == y ", is that operator== is not necessarily defined: equality is not a requirement of Assignable. If the type X is EqualityComparable as well as Assignable, then a copy of x should compare equal to x.
DefaultConstructible
Category: utilities
Component type: concept
A type is DefaultConstructible if it has a default constructor, that is, if it is possible to construct an object of that type without initializing the object to any particular value.
X
A type that is a model of DefaultConstructible
x
An object of type X
Name | Expression | Return type |
---|---|---|
Default constructor | X() | X |
Default constructor | X x; [1] |
Name | Expression |
---|---|
Default constructor | X() |
Default constructor | X x; |
• int
• vector<double>
[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
Assignable
Category: utilities
Component type: concept
A type is EqualityComparable if objects of that type can be compared for equality using operator==, and if operator== is an equivalence relation.
X
A type that is a model of EqualityComparable
x, y, z
Object of type X
Name | Expression | Return type |
---|---|---|
Equality | x == y | Convertible to bool |
Inequality | x != y | Convertible to bool |
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) |
Identity | &x == &y implies x == y |
Reflexivity | x == x |
Symmetry | x == y implies y == x |
Transitivity | x == y and y == z implies x == z |
• int
• vector<int>
LessThanComparable.
Category: utilities
Component type: concept
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.
X
A type that is a model of LessThanComparable
x, y, z
Object of type X
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.
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 |
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] |
Irreflexivity | x < x must be false. |
Antisymmetry | x < y implies !(y < x) [2] |
Transitivity | x < y and y < z implies x < z [3] |
• int
[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.
EqualityComparable, StrictWeakOrdering
Category: utilities
Component type: function
template <class T>
bool operator!=(const T& x, const T& y);
template <class T>
bool operator>(const T& x, const T& y);
template <class T>
bool operator<=(const T& x, const T& y);
template <class T>
bool operator>=(const T& x, const T& y);
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).
Defined in the standard header utility, and in the nonstandard backward-compatibility header function.h.
The requirement for operator!= is that x == y is a valid expression for objects x and y of type T.
The requirement for operator> is that y < x is a valid expression for objects x and y of type T.
The requirement for operator<= is that y < x is a valid expression for objects x and y of type T.
The requirement for operator>= is that x < y is a valid expression for objects x and y of type T.
The precondition for operator!= is that x and y are in the domain of operator==.
The precondition for operator>, operator<=, and operator>= is that x and y are in the domain of operator<.
template <class T>
void relations(T x, T y) {
if (x == y) assert(!(x != y));
else assert(x != y);
if (x < y) {
assert(x <= y);
assert(y > x);
assert(y >= x);
} else if (y < x) {
assert(y <= x);
assert(x < y);
assert(x <= y);
} else {
assert(x <= y);
assert(x >= y);
}
}
Equality Comparable, LessThan Comparable
Category: utilities
Component type: type
Pair<T1,T2> is a heterogeneous pair: it holds one object of type T1 and one of type T2. A pair is much like a Container, in that it "owns" its elements. It is not actually a model of Container, though, because it does not support the standard methods (such as iterators) for accessing the elements of a Container.
Functions that need to return two values often return a pair.
pair<bool, double> result = do_a_calculation();
if (result.first) do_something_more(result.second);
else report_error();
Defined in the standard header utility, and in the nonstandard backward-compatibility header pair.h.
Parameter | Description |
---|---|
T1 | The type of the first element stored in the pair |
T2 | The type of the second element stored in the pair |
Assignable
T1 and T2 must both be models of Assignable. Additional operations have additional requirements. Pair's default constructor may only be used if both T1 and T2 are DefaultConstructible, operator== may only be used if both T1 and T2 are EqualityComparable, and operator< may only be used if both T1 and T2 are LessThanComparable.
None.
Member | Where defined | Description |
---|---|---|
first_type | pair | See below. |
second | type pair | See below. |
pair() | pair | The default constructor. See below. |
pair(const first_type&, const second_type&) | pair | The pair constructor. See below. |
pair(const pair&) | Assignable | The copy constructor |
pair& operator=(const pair&) | Assignable | The assignment operator |
first | pair | See below. |
second | pair | See below. |
bool operator==(const pair&, const pair&) | pair | See below. |
bool operator<(const pair&, const pair&) | pair | See below. |
template <class T1, class T2> pair<T1, T2> make_pair(const T1&, const T2&) | pair | See below. |
These members are not defined in the Assignable requirements, but are specific to pair.
Member | Description |
---|---|
first_type | The type of the pair's first component. This is a typedef for the template parameter T1 |
second_type | The type of the pair's second component. This is a typedef for the template parameter T2 |
pair() | The default constructor. It uses constructs objects of types T1 and T2 using their default constructors. This constructor may only be used if both T1 and T2 are DefaultConstructible. |
pair(const first_type& x, const second_type& y) | The pair constructor. Constructs a pair such that first is constructed from x and second is constructed from y. |
first | Public member variable of type first_type: the first object stored in the pair. |
second | Public member variable of type second_type: The second object stored in the pair. |
template <class T1, class T2> bool operator==(const pair<T1,T2>& x, const pair<T1,T2>& y); | The equality operator. The return value is true if and only the first elements of x and y are equal, and the second elements of x and y are equal. This operator may only be used if both T1 and T2 are EqualityComparable . This is a global function, not a member function. |
template <class T1, class T2> bool operator<(const pair<T1,T2>& x, const pair<T1,T2>& y); | The comparison operator. It uses lexicographic comparison: the return value is true if the first element of x is less than the first element of y, and false if the first element of y is less than the first element of x. If neither of these is the case, then operator< returns the result of comparing the second elements of x and y. This operator may only be used if both T1 and T2 are LessThanComparable . This is a global function, not a member function. |
template <class T1, class T2> pair<T1, T2> make_pair(const T1& x, const T2& x) | Equivalent to pair<T1, T2>(x, y). This is a global function, not a member function. It exists only for the sake of convenience. |
Assignable, Default Constructible, LessThan Comparable