<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<style type="text/css">
body { color: #000000; background-color: #FFFFFF; }
del { text-decoration: line-through; color: #8B0040; }
ins { text-decoration: underline; color: #005100; }
p.example { margin: 2em; }
pre.example { margin: 2em; }
div.example { margin: 2em; }
code.extract { background-color: #F5F6A2; }
pre.extract { margin: 2em; background-color: #F5F6A2;
  border: 1px solid #E1E28E; }
p.function { }
p.attribute { text-indent: 3em; }
blockquote.std { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1; padding: 0.5em; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC; padding: 0.5em; }
blockquote.stdins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3; padding: 0.5em; }
table { border: 1px solid black; border-spacing: 0px;
  margin-left: auto; margin-right: auto; }
th { text-align: left; vertical-align: top; padding: 0.2em; border: none; }
td { text-align: left; vertical-align: top; padding: 0.2em; border: none; }
</style>

<title>Comparison in C++: Basic Facilities</title>
</head>
<body>
<h1>Comparison in C++: Basic Facilities</h1>

<p>
Committee: ISO/IEC JTC1 SC22 WG21 EWG Evolution<br>
Document Number: P0474r0<br>
Title: Comparison in C++ Basic Facilities<br>
Date: 2016-10-15<br>
Authors: Lawrence Crowl<br>
Reply To: Lawrence Crowl, Lawrence@Crowl.org<br>
Audience: EWG Evolution
</p>


<h2>Abstract</h2>

<p>
We propose functions that explicitly implement
partial, weak and total orders on basic types.
This proposal is the first step towards full implementation
of explicit order requirements in C++.
</p>


<h2>Contents</h2>

<p>
<a href="#Introduction">Introduction</a><br>
<a href="#Wording">Wording</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#order">?.? Order [order]</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#order.general">?.?.1 General [order.general]</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#order.function">?.?.2 Enumerations [order.enum]</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#order.function">?.?.3 Order Functions [order.function]</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#order.boolean">?.?.4 Boolean Functions [order.boolean]</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#order.boolean">?.?.5 Specializations [order.special]</a><br>
</p>


<h2><a name="Introduction">Introduction</a></h2>

<p>
In
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0100r1.html">P0100r1 Comparison in C++</a>,
we outlined an approach to ensuring that
standard algorithm ordering requirements are routinely met.
This paper proposes the first step in that approach,
the definition of order functions for basic types.
</p>

<h2><a name="Wording">Wording</a></h2>

<p>
The wording is as follows.
</p>


<h3><a name="order">?.? Order [order]</a></h3>

<p>
Add a new section.
</p>


<h3><a name="order.general">?.?.1 General [order.general]</a></h3>

<p>
Add a new section.
</p>

<blockquote class="stdins">

<p>
Order is defined by arithmetic relations
of equivalence and precedence.
</p>

<p>
All equivalence and precedence relations are transitive
(<var>a</var>?<var>b</var> and
<var>b</var>?<var>c</var> implies 
<var>a</var>?<var>c</var>)

<p>
All equivalence relations are reflexive
(<var>a</var>~<var>a</var>)
and symmetric
(<var>a</var>~<var>b</var> implies
<var>b</var>~<var>a</var>).
An <dfn>equality</dfn> (or congruence) relation is an equivalence relation
that is also substitutable
(<var>a</var> = <var>b</var> implies
<var>f</var>(<var>a</var>) = <var>f</var>(<var>b</var>)).
</p>

<p>
Precedence is irreflexive
(not <var>a</var>&lt;<var>a</var>),
i.e. an element is not less than itself.
Precedence is also asymmetric
(<var>a</var>&lt;<var>b</var> implies
not <var>b</var>&lt;<var>a</var>).
These axioms together constitute a <dfn>partial precedence</dfn>.
A <dfn>weak precedence</dfn>
also partitions the elements into ordered equivalence classes,
that is being unordered with respect to an element is a transitive relation
(<var>a</var>&lt;<var>b</var> implies
<var>a</var>&lt;<var>c</var> or
<var>c</var>&lt;<var>b</var>).
A <dfn>total precedence</dfn> is trichotomous
(exactly one of <var>a</var>&lt;<var>b</var>,
<var>a</var>=<var>b</var>,
<var>a</var>&gt;<var>b</var>)
where equality is defined above.
</p>

<p>
The precedence and equivalence relations defined above
provide for three classes of <dfn>order</dfn>,
partial, weak and total.
A object is <dfn>less</dfn> than another when it precedes the other.
A object is <dfn>greater</dfn> than another when the other precedes it.
Otherwise, the objects are <dfn>unordered</dfn>, <dfn>equivalent</dfn>,
or <dfn>equal</dfn>
according to whether the precedence relation is partial, weak, or total.
</p>

<p>
A boolean function <var>g</var>
<dfn>refines</dfn> a boolean function <var>f</var>
when for every <var>a</var> and <var>b</var>,
<var>f</var>(<var>a</var>,<var>b</var>) implies
<var>g</var>(<var>a</var>,<var>b</var>).
That is, if a call to <var>f</var> is true,
so is the corresponding call to <var>g</var>.
</p>

<p>
An order is <dfn>consistent</dfn> with another order
when the its precedence relation refines the other's precedence relation.
</p>

</blockquote>


<h3><a name="order.function">?.?.2 Enumerations [order.enum]</a></h3>

<p>
Add a new section.
</p>

<blockquote class="stdins">

<p>
<code>enum class partial_ordering { less, unordered, greater };<br>
enum class weak_ordering { less, equivalent, greater };<br>
enum class total_ordering { less, equal, greater };</code>
</p>

<p>
The enumerations <code>partial_ordering</code>,
<code>weak_ordering</code>, and <code>total_ordering</code>
describe the order between two objects as defined in [order.general].
</p>

</blockquote>


<h3><a name="order.function">?.?.3 Order Functions [order.function]</a></h3>

<p>
Add a new section.
</p>

<blockquote class="stdins">

<p class="function">
<code>template &lt;typename T&gt;<br>
partial_ordering partial_order(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
The partial order between the two objects.
</p></dd>
<dt>Constraints:</dt>
<dd><p>
The order should be consistent with that implied by the comparison operators,
or vice-versa.
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
weak_ordering weak_order(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
The weak order between the two objects.
</p></dd>
<dt>Constraints:</dt>
<dd><p>
The order shall be consistent with that provided by <code>partial_order</code>.
The order should be consistent with that implied by the comparison operators,
or vice-versa.
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
total_ordering total_order(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
The total order between the two objects.
</p></dd>
<dt>Constraints:</dt>
<dd><p>
The order shall be consistent with that provided by <code>weak_order</code>.
The order should be consistent with that implied by the comparison operators.
</p></dd>
</dl>

</blockquote>


<h3><a name="order.boolean">?.?.4 Boolean Functions [order.boolean]</a></h3>

<p>
Add a new section.
</p>

<blockquote class="stdins">

<p>
Boolean functions provide potentially more efficient conditional execution.
</p>

<p class="function">
<code>template &lt;typename T&gt;<br>
bool partial_less(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
as if <code>partial_ordering::less == partial_order(a, b)</code>
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
bool weak_less(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
as if <code>weak_ordering::less == weak_order(a, b)</code>
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
bool total_less(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
as if <code>total_ordering::less == total_order(a, b)</code>
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
bool partial_unordered(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
as if <code>partial_ordering::unordered == partial_order(a, b)</code>
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
bool weak_equivalent(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
as if <code>weak_ordering::equivalent == weak_order(a, b)</code>
</p></dd>
</dl>

<p class="function">
<code>template &lt;typename T&gt;<br>
bool total_equal(const T&amp; a, const T&amp; b);</code>
</p>

<dl class="attribute">
<dt>Returns:</dt>
<dd><p>
as if <code>total_ordering::equal == total_order(a, b)</code>
</p></dd>
</dl>

</blockquote>


<h3><a name="order.boolean">?.?.5 Specializations [order.special]</a></h3>

<p>
Add a new section.
</p>

<blockquote class="stdins">

<p>
The implementation shall provide specializations
for all integral, floating-point and pointer types.
A negative zero shall be <code>total_ordering::less</code> than a positive zero.
If for a type <code>T</code>,
<code>std::numeric_limits&lt;T&gt;::is_iec559</code> is true,
then the corresponding <code>total_order</code> function
shall match that of the <code>totalOrder</code> function in IEC 60559.
</p>

</blockquote>

</body></html>
