Utility (C++)


' 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 of arbitrary type. Additionally, the header defines default relational operators for s which have both types in common.

's implementation declares the namespace in a manner similar to the following:

namespace std::rel_ops

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 ;
void f1

By invoking the templates, one can assign a default meaning to the remaining relational operators. However, if a similar type-specific operator exists in the current scope, even outside the class definition, the compiler will prefer it instead.

//
// below operator supersedes rel_ops
bool operator>= ;
void f2

One could of course declare the following in tandem with, allowing the derivation of all relational operators from :

template
inline bool operator

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 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

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

Additionally the header contains a template-function which deduces its return type based on parameters:

namespace std