Utility (C++)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 4 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

is a header file in the C++ Standard Library. This file has two key components: * , a namespace containing set of templates which define default behavior for the relational operators , , , and between objects of the same type, based on user-defined operators and . * , a container template which holds two member objects ( and ) of arbitrary type(s). Additionally, the header defines default relational operators for s which have both types in common.

is a header file in the C++ Standard Library. This file has two key components: , a namespace containing set of templates which define default behavior for the relational operators , , , and between objects of the same type, based on user-defined operators and . , a container template which holds two member objects ( and ) of arbitrary type(s). Additionally, the header defines default relational operators for s which have both types in common.

GCC's implementation declares the namespace in a manner similar to the following: namespace std::rel_ops { template inline bool operator!=(const T& x, const T& y) { return !(x == y); } template inline bool operator>(const T& x, const T& y) { return y inline bool operator inline bool operator>=(const T& x, const T& y) { return !(x Consider the following declaration of , which defines equality and less-than operators for comparison against other objects of the same type: import std; using namespace std::rel_ops; class BuildingLocation { unsigned int building; unsigned int room; public: bool operator

(const BuildingLocation& other) const { return (building

other.building) && (room == other.room); } bool operator a2; // error: no match for ‘operator >’ in ‘a1 > a2’ bool less_equal = a1 = a2; // error: no match for ‘operator>=’ in ‘a1 >= a2’ } By invoking the templates, one can assign a default meaning to the remaining relational operators. However, if a similar type-specific (i.e. non-template) operator exists in the current scope, even outside the class definition, the compiler will prefer it instead. // (continued from above) // below operator supersedes rel_ops bool operator>=(const BuildingLocation& a1, const BuildingLocation& a2) { do_something_else(); // perform some distinguishing side-effect return !(a1 a2; // uses (a2 = a2; // uses global operator >= defined above } One could of course declare the following in tandem with , allowing the derivation of all relational operators from : template inline bool operator

(const T& x, const T& y) { return !(x

== is a struct that encapsulates two objects of two types. An object declared, for example, as will contain two members, and , plus three constructor functions. The first (default) constructor initializes both members with the default values and , whereas the second one accepts one parameter of each type. The third is a template copy-constructor which will accept any , provided the types and are capable of implicit conversion to and respectively. The implementation of std::pair looks roughly similar to the following: namespace std { template struct pair { using first_type = T1; using second_type = T2; T1 first; T2 second; pair(): first(), second() {} pair(const T1& a, const T2& b): first(a), second(b) {} template pair(const pair & p): first(p.first), second(p.second) {} }; } Additionally this header defines all six relational operators for instances with both types in common. These define a strict weak ordering for objects of type , based on the elements and then upon the elements only when the ones are equal. namespace std { template inline bool operator

(const pair & x, const pair & y) { return x.first

y.first && x.second == y.second; } template inline bool operator & x, const pair & y) { return x.first inline bool operator!=(const pair & x, const pair & y) { return !(x == y); } template inline bool operator>(const pair & x, const pair & y) { return y inline bool operator & x, const pair & y) { return !(y inline bool operator>=(const pair & x, const pair & y) { return !(x Additionally the header contains a template-function which deduces its return type based on parameters: namespace std { // continued from above template inline pair make_pair(T1 x, T2 y) { return pair (x,y); } }

See also

Relational operator

References

External links

C++ reference for general utilities library

Text derived from https://en.wikipedia.org/w/index.php?oldid=1339984161 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001339984161 · captured 1771829601.0 · version history

Cite this page as: encyclopegia.org, “Utility (C++)”, snapshot enwiki-001339984161. https://encyclopegia.org/a/utility-c

Sources cited in this revision

Extracted from the cited revision's reference blocks; absent citations are not invented.