BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslalg_synththreewayutil

Outline

Purpose

Provide implementation utilities for operator<=>.

Classes

See also
bslstl_compare

Description

This component provides a namespace class, bslalg::SynthThreeWayUtil, that contains an implementation of the Standard exposition-only entities (see [expos.only.entity] in the C++20 Standard): synth-three-way (bslalg::SynthThreeWayUtil::compare) and synth-three-way-result (bslalg::SynthThreeWayUtil::Result).

bslalg::SynthThreeWayUtil::compare is a callable object with the following effective signature:

template <class T1, class T2>
constexpr Result<T1, T2> compare(const T1& t1, const T2& t2);

It returns the result of t1 <=> t2 expression if this expression is valid. Otherwise, if t1 < t2 and t2 < t1 expressions are well-formed and return boolean-testable results, operator< is used to emulate operator<=>. Otherwise this function is not defined and does not participate in overload resolution.

As shown above, bslalg::SynthThreeWayUtil::Result<T1, T2> is a type returned by bslalg::SynthThreeWayUtil::compare<T1, T2>.

Usage

This section illustrates intended use of this component.

Example 1: Implementing <=> For a Sequence Container {#bslalg_synththreewayutil-example-1-implementing-<=>-for-a-sequence-container}

In the following example we use bslalg::SynthThreeWayUtil to implement the three-way comparison operator (<=>) for a list container (whose details have been elided):

template <class T, class A>
bslalg::SynthThreeWayUtil::Result<T> operator<=>(const list<T,A>& lhs,
const list<T,A>& rhs)
{
return bsl::lexicographical_compare_three_way(
lhs.begin(),
lhs.end(),
rhs.begin(),
rhs.end(),
bslalg::SynthThreeWayUtil::compare);
}

Now the <, >, <= and >= operators are automatically defined for the container:

// Create some instance of the template
list<int> list1, list2;
// Empty lists are equal
assert(!(list1 < list2));
assert(!(list1 > list2));
assert( list1 <= list2);
assert( list1 >= list2);