cpp/language/operator comparison

Compares the arguments.

Two-way comparison
The two-way comparison operator expressions have the form

@1@ Returns if  is less than,  otherwise. @2@ Returns if  is greater than,  otherwise. @3@ Returns if  is less than or equal to,  otherwise. @4@ Returns if  is greater than or equal to,  otherwise. @5@ Returns if  is equal to,  otherwise. @6@ Returns if  is not equal to,  otherwise.

In all cases, for the built-in operators, and  must have either after the application of the, and  standard conversions.
 * arithmetic or enumeration type (see arithmetic comparison operators below)
 * pointer type (see pointer comparison operators below)

In any case, the result is a prvalue.

Arithmetic comparison operators
If the operands have arithmetic or enumeration type (scoped or unscoped), are performed on both operands following the rules for. The values are compared after conversions:

Pointer comparison operators
Comparison operators can be used to compare two pointers.

Only equality operators ( and ) can be used to compare the following pointer pairs:
 * two pointers-to-members
 * a null pointer constant with a pointer or a pointer-to-member

First, (pointer to member conversions if the arguments are pointers to members) and  are applied to both operands to obtain the composite pointer type, as follows:

@2@ If one operand is a null pointer constant, and the other is a pointer, the composite type is exactly the pointer type.

@3@ If the operands are the composite type is "pointer to cv12 ", where cv12 is the union of cv1 and cv2.
 * a pointer to cv1, and
 * a pointer to cv2, where is an object type or ,

@4@ If the types of the operands are and if is the same as  or is a base class of, then the composite pointer type is the cv-combined type of  and. Otherwise, if is a base class of, then the composite pointer type is the cv-combined type of  and.
 * , a pointer to (possibly cv-qualified), and
 * , a pointer to (possibly cv-qualified) ,

@5@ If the types of the operands are and if is the same as or derived from, then the composite pointer type is the cv-combined type of  and. Otherwise, if is derived from, then the composite pointer type is the cv-combined type of  and.
 * , pointer to member of of type (possibly cv-qualified), and
 * , pointer to member of of type (possibly cv-qualified) ,

@6@ If the types of the operands and  are multi-level mixed pointer and pointer to member types with the same number of levels that only differ by cv-qualifications at any of the levels, the composite pointer type is the cv-combined type of  and.

In the definition above, cv-combined type of two pointer types and  is a type  that has the same number of levels and type at every level as, except that cv-qualifications at every level are set as follows: @a@ at every level other than top level, the union of the cv-qualifications of and  at that level, @b@ if the resulting cv-qualification at any level is different from 's or 's cv-qualification at the same level, then is added to every level between the top level and this one.

For example, the composite pointer type of and  is. The composite pointer type of and  is. Note that until the resolution of,  and  could not be compared.

Note that this implies that any pointer can be compared with.

The result of comparing two pointers to objects (after conversions) is defined as follows:

@1@ If two pointers point to different elements of the same array, or to subobjects within different elements of the same array, the pointer to the element with the higher subscript compares greater. In other words, the results of comparing the pointers is the same as the result of comparing the indexes of the elements they point to. @2@ If one pointer points to an element of an array, or to a subobject of the element of the array, and another pointer points one past the last element of the array, the latter pointer compares greater. Pointers to non-array objects are treated as pointers to arrays of one: compares greater than. @3@ If, within an object of non-union class type, two pointers point to different non-static data members, or to subobjects or array elements of such members, recursively, the pointer to the later declared member compares greater. In other words, class members are positioned in memory in order of declaration.

The result of equality comparison of two pointers (after conversions) is defined as follows: @1@ If the pointers are both null pointer values, they compare equal. @2@ If the pointers are pointers to function and point to the same function, they compare equal. @3@ If the pointers are pointers to object and represent the same address, they compare equal (this includes two pointers to non-static members of the same union, pointers to standard-layout struct and to its first member, pointers related by, etc). @4@ All other pointers compare unequal.

The result of comparing two pointers to members (after conversions) is defined as follows: @1@ If both pointers to member are null member pointer values, they compare equal. @2@ Otherwise, if only one of two pointers to member is the null member pointer value, they compare unequal. @3@ Otherwise, if either is a pointer to a virtual member function, the result is unspecified. @4@ Otherwise, if one refers to a member of class and the other refers to a member of a different class, where neither is a base class of the other, the result is unspecified.

@5@ Otherwise, if both refer to (possibly different) members of the same union, they compare equal. @6@ Otherwise, two pointers to member compare equal if and only if they would refer to the same member of the same most derived object or the same subobject if they were dereferenced with a hypothetical object of the associated class type. @7@ Otherwise they compare unequal.

If a pointer compare equal to pointer,  and  both yield  and  and  both yield.

If a pointer compares greater than a pointer, then , , , and  all yield  and , , , and  all yield.

If two pointers are not specified to compare greater or compare equal, the result of the comparison is unspecified. An unspecified result may be nondeterministic, and need not be consistent even for multiple evaluations of the same expression with the same operands in the same execution of the program:

In, for every pair of promoted arithmetic types and , including enumeration types, the following function signatures participate in overload resolution:

For every type which is either pointer to object or pointer to function, the following function signatures participate in overload resolution:

For every type that is a pointer to member object or pointer to member function or std, the following function signatures participate in overload resolution:

Three-way comparison
The three-way comparison operator expressions have the form

The expression returns an object such that
 * if ,
 * if ,
 * if and  are equal/equivalent.

If one of the operands is of type and the other is not, the program is ill-formed.

If both operands have arithmetic types, or if one operand has unscoped enumeration type and the other has integral type, the usual arithmetic conversions are applied to the operands, and then


 * If a narrowing conversion is required, other than from an integral type to a floating point type, the program is ill-formed.
 * Otherwise, if the operands have integral type, the operator yields a prvalue of type :
 * std if both operands are arithmetically equal,
 * std if the first operand is arithmetically less than the second,
 * std otherwise.


 * Otherwise, the operands have floating-point type, and the operator yields a prvalue of type . The expression yields
 * std if is less than ,
 * std if is greater than ,
 * std if is equivalent to  ( is equivalent),
 * std ( is unordered).

If both operands have the same enumeration type, the operator yields the result of converting the operands to the underlying type of E and applying to the converted operands.

If at least one of the operands is a pointer or pointer-to-member, array-to-pointer conversions, derived-to-base pointer conversions, function pointer conversions, and qualification conversions are applied as necessary to convert both operands to the same pointer type, and the resulting pointer type is an object pointer type, returns a prvalue of type :
 * std if ,
 * std if ,
 * std if ,
 * unspecified result if comparison is unspecified for these pointer values (such as when they do not point into the same object or array).

Otherwise, the program is ill-formed.

In, for pointer or enumeration type , the following function signature participates in overload resolution:

Where is the ordering category type defined above.

Standard library
Comparison operators are overloaded for many classes in the standard library.

The namespace provides generic operators, , , and :