﻿<html>

<head>
    <meta http-equiv="Content-Language" content="en-us">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Defaulted comparison operators</title>
    <style type="text/css">
        body {
            font-size: 11pt;
            font-family: Verdana, Sans-Serif;
        }
        p {
        }
        blockquote {
            border: solid thin;
            padding: 1em 1em 1em 1em;
        }

        blockquote.stdins {
            background-color: #CEFFCE;
        }

        blockquote.std {
            background-color: #E5E5E7;
        }

        ins {
            color: #005100;
        }

        li {
            padding-bottom: 0.3em;
        }

        pre {
            background-color: #F7F6dc;
            margin-left: 3em;
            margin-right: 3em;
            padding: 1em 1em 1em 1em;
            font-size: 90%;
        }
    </style>
</head>

<body>

  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="619">
    <tr>
      <td width="167" align="left" valign="top">Document number:</td>
      <td width="452">N4114</td>
    <tr>
      <td width="167" align="left" valign="top">Date:</td>
      <td width="452">2014-07-02</td>
    </tr>
    <tr>
      <td width="167" align="left" valign="top">Project:</td>
      <td width="452">Programming Language C++, Language Evolution Working Group</td>
    </tr>
    <tr>
      <td width="167" align="left" valign="top">Reply-to:</td>
      <td width="452">Oleg Smolsky <a href="mailto:oleg.smolsky@gmail.com">oleg.smolsky@gmail.com</a></td>
    </tr>
  </table>

    <h1>Defaulted comparison operators</h1>

    <h2>Revision history</h2>
        <p><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3950.html">N3950</a>
        was presented to the Evolution WG at the Rapperswil meeting and the response was very
        positive. 

        <p>Changes requested during the meeting and implemented in the updated proposal:
        <ul>
        <li>Non-member operators were unanimously requested
        <li>Mutable members: there was consensus on their treatment. The compromise is to
            to make explicitly defaulted operators ill-formed when mutable members are present.
            See <a href="#mutable">"Mutable treatment"</a>.
        <li>Short-hand notation was proposed and had very positive feedback.
            See <a href="#shorthand">"The proposed syntax: short form"</a>.
        </ul>

        <p>Changes that came from the technical review on the <i>c++std-ext</i> list:
        <ul>
        <li>Pointer and enumerated types are handled by the generated (in)equality operators
        <li>Floating point members prevent generation of the explicitly defaulted operators.
            See <a href="#nonintegral">"Members of non-integral types"</a> for the discussion
            and other options.
        <li>Lexicographical comparison is defined explicitly
        <li>The technical specification contains rational and examples now
        </ul>

    <h2>I. Introduction</h2>
    <p>Equality for composite types is an age-old problem which effects an overwhelming chunk
       of the programming community. Specifically, the users that program with
       <i>Regular</i><sup><a href="#regular">1</a></sup> types and 
       expect that they would compose naturally.

    <p>The proposal presents means of generating default equality/inequality for <i>Regular</i>, as well
       as relational operators for <i>Totally Ordered</i><sup><a href="#totallyordered">2</a></sup>
       user-defined types. Such types should 
       be trivial to implement and easy to understand. 

    <p>Specifically, I argue that:
    <ol>
    <li>The presence of <code>operator==()</code> implies that the
       type is Regular. I expect that the equality implemented fully (ie it is transitive, reflexive and holds for copies).

    <li>The presence of <code>operator&lt;()</code> implies that the type is Totally Ordered.
    </ol>

    <p>Finally, the feature is strictly "opt in" so that semantics of existing code remain intact.

    <h2>II. Motivation and scope</h2>

    <p>Simple means of doing equality are really
       useful in modern C++ code that operates with types
       composed of Regular members. The definition of equality is trivial in such cases -
       member-wise comparison. Inequality should then be generated as its boolean negation.

    <p>This proposal focuses on Regular and Totally Ordered types as they naturally compose. Such
       cases are becoming more prevalent as people program more with value types and so writing
       the same equality and relational operators becomes tiresome. This is especially true when 
       trying to lexicographically compare members to achieve total ordering.

    <p>Consider the following trivial example where a C++ type represents some kind of
        user record:

        <pre>
struct user
{
    uint32_t id, rank, position;

    std::string first_name, last_name;

    std::string address1, address2, city, state, country;
    uint32_t us_zip_code;

    friend bool operator==(const user &amp;, const user &amp;);
    friend bool operator!=(const user &amp;, const user &amp;);

    friend bool operator&lt;(const user &amp;, const user &amp;);
    friend bool operator&gt;=(const user &amp;, const user &amp;);
    friend bool operator&gt;(const user &amp;, const user &amp;);
    friend bool operator&lt;=(const user &amp;, const user &amp;);
};</pre>

        <h3>Verbosity</h3>
        The structure consists of regular members and the implementation of the equality operator 
        is trivial yet verbose:

        <pre>
bool operator==(const user &amp;a, const user &amp;b)
{
    <b>return</b> a.id == b.id &amp;&amp;
           a.rank == b.rank &amp;&amp;
           a.position == b.position &amp;&amp;
           a.address1 == b.address1 &amp;&amp;
           a.address2 == b.address2 &amp;&amp;
           a.city == b.city &amp;&amp;
           a.state == b.state &amp;&amp;
           a.country == b.country &amp;&amp;
           a.us_zip_code == b.us_zip_code;
}</pre>

<p>Also, the composite type is naturally Totally Ordered, yet that takes even more code:
<pre>
bool operator&lt;(const user &amp;a, const user &amp;b)
{
    <i style="color: green;">// I could implement the full lexicographical comparison of members manually, yet I 
    // choose to cheat by using standard libraries</i>
    <b>return</b> std::tie(a.id, a.rank, a.position,
                    a.address1, a.address2, a.city, a.state, a.country, 
                    a.us_zip_code)
           &lt;
           std::tie(b.id, b.rank, b.position, 
                    b.address1, b.address2, b.city, b.state, b.country,
                    b.us_zip_code);
}</pre>

        Specifically, this code, while technically required, suffers from the following issues:
        <ul>
        <li>needlessly verbose - every member is already comparable (Regular and Totally Ordered)
        <li>error prone - the author could miss a member and  the implementation may become stale 
            when a new member is added
        <li>not in the spirit of Modern C++: correct things should be intuitive and easy
        </ul>

        <h3>Correctness</h3>

        <p>It is vital that equal/unequal, less/more-or-equals and more/less-or-equal pairs
        behave as boolean negations of each other. After all, we are building total ordering
        and the world would make no sense
        if both <code>operator==()</code> and <code>operator!=()</code> returned <i>false</i>!
        
        <p>As such, it is common to implement these operators in terms of each other.

        <p>Inequality for Regular types:

        <pre>
bool operator!=(const user &amp;a, const user &amp;b) 
{
    return !(a == b);
}</pre>

        <p>Relational operators for Totally Ordered types:
<pre>
bool operator&gt;=(const user &amp;a, const user &amp;b)
{
    return !(a &lt; b);
}

bool operator&gt;(const user &amp;a, const user &amp;b)
{
    return b &lt; a;
}

bool operator&lt;=(const user &amp;a, const user &amp;b)
{
    return !(a &gt; r);
}</pre>

        Notes:
        <ul>
        <li>Based on my experience, these templates are applicable to the vast
            majority of types that define comparison operators (as the members 
            are very often regular and totally ordered)
        <li>The users should have some kind of "short hand" for stating what is common, 
            correct and simple
        <li>These relations are algebraic in nature and must be considered carefully.
            For example,
            <code>operator&lt;()</code> must remain transitive in its nature.
        </ul>



    <h2>III. Design decisions</h2>
    <h3>The proposed syntax: long form</h3>
        <p>Member-wise generation of special functions is already present in the Standard 
        (see Section 12), so it seems natural to extend the scope of generation and reuse
        the existing syntax.

        <p>The proposed syntax for generating the new explicitly defaulted non-member
           operators is as follows:
    <pre class="code">
struct Thing
{
    int a, b, c;
    std::string d;
};

bool operator==(const Thing &amp;, const Thing &amp;)<b>= default</b>;
bool operator!=(const Thing &amp;, const Thing &amp;)<b>= default</b>;
</pre>

    <p>There are cases where members are private and so the operators need to be declared as
    <b>friend</b>. Consider the following syntax:
    <pre class="code">
class AnotherThing
{
    int a, b;

public:
    <i style="color: green;">// ...</i>

    <b>friend</b> bool operator&lt;(Thing, Thing)<b> = default</b>;
    <b>friend</b> bool operator&gt;(Thing, Thing)<b> = default</b>;
    <b>friend</b> bool operator&lt;=(Thing, Thing)<b> = default</b>;
    <b>friend</b> bool operator&gt;=(Thing, Thing)<b> = default</b>;
};
</pre>

<p>I feel this is a natural choice because:
       <ul>
            <li>C++11 already has member function specifiers such as <b>default</b> and 
                <b>delete</b>
            <li>Users can "opt in" to get the new behavior
            <li>The new functions are explicitly declared using a familiar syntax, so users
                get a feel of what they are asking for
            <li>The user can specify whether the parameters are taken by value or by reference
       </ul>

    <a name="shorthand">
    <h3>The proposed syntax: short form</h3></a>
        <p>Several committee members expressed strong support for a shorter
           form that would radically reduce the amount of code it takes
           to declare the non-member operators. Here is the short-hand that
           extends to the long form defined above.

        <pre class="code">
struct Thing
{
    int a, b, c;
    std::string d;

    default: ==, !=, &lt;, &gt;, &lt;=, &gt;=;<i style="color: green;">   // defines the six non-member functions</i>
};</pre>
    
    <p>Notes on the light short-hand notation: 
       <ul>
            <li>Radical reduction in boiler-plate that is otherwise be copied and pasted
            <li>The compiler will choose the argument types for correctness and performance
       </ul>


    <h3>Other points of consideration</h3>
    <p>It is possible to mandate that every explicitly defaulted operator is to be implemented
       in a member-wise fashion. In fact, it would we consistent with copy construction,
        assignment and equality. However:
        <ul>
            <li>Such an implementation is not useful. It would generate useless code which
                may be sizable due to the number of members.
            <li>The most logical choice for inequality is the boolean negation of equality. The
                same applies to the other operators - they can all be derived.
            <li>Users are still free to implement the operators in any way they see fit - the
                defaulted functions are "opt in" and strive to simplify the common case
                where users deal with regular types.
        </ul>

    <a name="mutable">
    <h3>Mutable treatment</h3></a>
    <p>The Evolution WG was divided on the <code>mutable</code> treatment. There were
       two mutually exclusive views:
        <ol>
        <li>Exclude <code>mutable</code> members from the comparison operators.<br>
            - such members were invented for caching data derived from the object's state<br>
            - as such, they are not a part of the value type and should not participate in comparisons
        <li>Include <code>mutable</code> members when doing comparisons (ie no special treatment)<br>
            - such implementation is consistent with special member functions: copy constructor and assignment operator
        </ol>

    <p>I prefer option (1) above, yet the only way to resolve the committee dead lock is to make
       code with such members ill-formed. The user would have to implement the comparison operators
       manually. The committee thus reserves an option to reconsider the decision at a later stage,
       as part of a follow up proposal.</p>

    <a name="nonintegral">
    <h3>Members of non-integral types</h3></a>
    <p>There are some built-in types that are not totally ordered or cannot always 
       be compared. Namely, "less than" is only defined for pointers of the same type 
       that refer to memory allocated from a single contiguous region, IEEE floating 
       point numbers have the NaN and the comparisons are defined in a very special way.

    <p>Design decisions:
    <ol>
        <li>Floating point members prevent generation of explicitly defaulted operators
        <li>Equality is defined for pointer types, so such members are supported when generating 
            explicitly equality/inequality operators
        <li>Relational operators are not always defined for pointers, so such members
            prevent generation of explicitly defaulted relational operators
    </ol>

    <p>Other option:
    <ul>
        <li>The technical specification could state that the generated equality and
            relational operators are only defined in the domain of the members' normal values.
            This would let the compiler generate code for pointers and floating point values,
            but put more responsibility on the users to avoid undefined behavior...
    </ul>

    <h2>IV. Technical specifications</h2>
    <h3>Edit section 8.4.2 "Explicitly-defaulted functions [dcl.fct.def.default]" </h3>

    <ol>
    <li><blockquote class="std">A function definition of the form:<br>
        <i>attribute-specifier-seq<sub>opt</sub> decl-specifier-seq<sub>opt</sub> declarator virt-specifier-seq<sub>opt</sub></i> = <b>default</b> ;<br>
        is called an explicitly-defaulted definition. A function that is explicitly defaulted shall<br>
        — be a special member function<ins>, or an explicitly defaultable operator
       function. See [defaultable]</ins></blockquote>
    </ol>

    <h3>New section in 8.4</h3>

    <p>After 8.4.3 add a new section
    <blockquote class="stdins">
    <b>8.4.4 Explicitly defaultable operator functions [defaultable]</b>
    <p>The following friend operator functions are explicitly defaultable:
        <ol>
        <li>Non-member equality operators: 
            <code>operator==()</code>, <code>operator!=()</code>, see [class.equality]
        <li>Non-member relational operators: 
            <code>operator&lt;()</code>, <code>operator&gt;()</code>,
            <code>operator&lt;=()</code>, <code>operator&gt;=()</code>, see [class.relational]
        </ol>
    </blockquote>

    <h3>Edit section "12 Special member function [special]" </h3>
    <p><blockquote class="std">The default constructor (12.1), copy constructor and copy assignment operator (12.8), 
        move constructor and move assignment operator (12.8) and destructor (12.4) are special
        member functions. 
        <ins>These, together with equality operators (12.10) and
        relational operators (12.11) may be explicitly defaulted as per 
        [dcl.fct.def.default]</ins></blockquote>

    <h3>New sections in 12</h3>

    <p>After 12.9 add a new section
    <blockquote class="stdins">
    <p><b>12.10 Equality operators [class.equality]</b>
    <ol>
        <li>A non-union class may provide overloaded <code>operator==()</code> and 
            <code>operator!=()</code> as per [over.oper].
            A default implementation these non-member operators may be
            generated via the <code>= default</code> notation as it may be explicitly
            defaulted as per [dcl.fct.def.default].

        <li>The defaulted <code>operator==()</code> definition is generated if and only if all 
            sub-objects and base classes are integral types, enumerated types, pointer
            types or user-defined types that provide <code>operator==()</code>, 
            as well as arrays of that.

        <li>If a class with a defaulted <code>operator==()</code> has a <code>mutable</code>
            member, the program is ill-formed

        <li>The defaulted <code>operator==()</code> for class X shall take two arguments
            of type X by value or by const reference and return bool.

        <li>The explicitly defaulted non-member <code>operator==()</code> for a non-union class X
            shall perform memberwise equality comparison of its subobjects. 
            Namely, a comparison of the subobjects that have the same position in both objects
            against each other until one subobject is not equal to the other.

            <p>Direct base classes of X are compared first, in the order of their declaration in the 
            base-specifier-list, and then the immediate non-static data members of X are compared,
            in the order in which they were declared in the class definition.

            <p>Let x and y be the parameters of the defaulted operator function. Each subobject
            is compared in the manner appropriate to its type:

            <ul>
                <li>if the subobject is of class type, as if by a call to <code>operator==()</code> with
                    the subobject of x and the corresponding subobject of y as a
                    function arguments (as if by explicit qualification; that is, ignoring any 
                    possible virtual overriding functions in more derived classes);

                <li>if the subobject is an array, each element is compared in the manner appropriate 
                    to the element type;

                <li>if the subobject is of a scalar type, the built-in "equality" operator is used.
            </ul>

        <li>The explicitly-defaulted non-member <code>operator!=()</code> for a non-union 
            class shall perform an operation equivalent to the equality comparison between 
            the two function parameters and then return a boolean negation of the result.
    </ol>

    <p>Example: <pre>struct T {
    int a, b, c;
    std::string d;
};

bool operator==(const T &amp;, const T &amp;) = default;</pre>

    <p>Note, floating point values are regular only in the domain of normal values
       (outside of the NaN), so such members make the explicitly defaulted 
       <code>operator==()</code> and <code>operator!=()</code> functions ill-formed.
       Users are free to implement equality manually in such cases.
</blockquote>

    <p>After 12.10 add a new section
    <blockquote class="stdins">
    <b>12.11 Relational operators [class.relational]</b>
    <ol>
        <li>A non-union class may provide overloaded relational operators as per [over.oper]. 
            A default implementation of a non-member relational operator may be generated
            via the <code>= default</code> notation as these may be explicitly defaulted
            as per [dcl.fct.def.default].

        <li>The defaulted <code>operator&lt;()</code> definition is generated if and only if all 
            sub-objects and base classes are integral types, enumerated types or 
            user-defined types that provide <code>operator&lt;()</code>, as well as arrays of that.

        <li>If a class with a defaulted <code>operator&lt;()</code> has a <code>mutable</code>
            member, the program is ill-formed

        <li>The defaulted <code>operator&lt;()</code> for class X shall take two arguments 
            of type X by value or by const reference and return bool.

        <li>The explicitly-defaulted <code>operator&lt;()</code> for a non-union class X shall
            perform memberwise lexicographical comparison of its subobjects. Namely,
            a comparison of the subobjects that have the same position in both objects against each
            other until one subobject is not equivalent to the other. The result of comparing these
            first non-matching elements is the result of the function.

            <p>Direct base classes of X are compared first, in the order of their declaration in the 
            base-specifier-list, and then the immediate non-static data members of X are compared,
            in the order in which they were declared in the class definition.

            <p>Let x and y be the parameters of the defaulted operator function. Each subobject 
            is compared in the manner appropriate to its type:

            <ul>
                <li>if the subobject is of class type, as if by a call to <code>operator&lt;()</code> with
                    the subobject of x and the corresponding subobject of y as a
                    function arguments (as if by explicit qualification; that is, ignoring any 
                    possible virtual overriding functions in more derived classes);

                <li>if the subobject is an array, each element is compared in the manner appropriate 
                    to the element type;

                <li>if the subobject is of a scalar type, the built-in "less than" operator is used.
            </ul>

        <li>An explicitly-defaulted non-member <code>operator&gt;=()</code> for a non-union
            class shall perform an operation equivalent to
            a "less than" comparison between the two function parameters (in the left-to-right order)
            and then return a boolean negation of the result

        <li>An explicitly-defaulted non-member <code>operator&gt;()</code> for a non-union
            class shall perform an operation equivalent to
            a "less than" comparison between the two function parameters (in the right-to-left order) 
            and then return the result

        <li>An explicitly-defaulted non-member <code>operator&lt;=()</code> for a non-union
            class shall perform an operation equivalent to
            a "greater than" comparisong between the two function parameters (in the left-to-right)
            order and then returns the result
    </ol>

    <p>Example: <pre>struct T {
    int a, b;

    friend bool operator&lt;(T, T) = default;
};</pre>

    <p>Note, pointer comparisons are only defined for a subset of values, floating point 
       values are totally ordered only in the domain of normal values (outside of the NaN), 
       so such members make the explicitly defaulted relational operators ill-formed. 
       Users are free to implement relational operator functions manually in such cases.
    </blockquote>

    <p>After 12.11 add a new section
    <blockquote class="stdins">
    <b>12.12 Explicitly defaulted equality and relational operators - short form [class.oper-short]</b>
    <ol>
        <li>A non-union class may provide explicitly defaulted equality and relational
            operators as per [class.equality] and [class.relational] respectively. 
            These non-member operators can also be generated via the short form of
            the notation:<br>
            <code>default: [the coma-separated list of operators];</code>
        <li>The following six short-hand names map to the explicitly defaultable
            equality and relational operators: <code>==, !=, &lt;, &lt;=, &gt;, &gt;=</code>.
        <li>The implementation must expand each term of the short form into a full
            declaration subject to [class.equality] and [class.relational], while
            choosing how to pass the arguments in order to maximize performance.
    </ol>

    <p>Example:
    <pre class="code">
struct Thing
{
    int a, b, c;
    std::string d;

    default: ==, !=;<i style="color: green;">   // defines equality/inequality non-member functions</i>
};</pre>
    </blockquote>

    <h2>V. Acknowledgments</h2>
        <p>I believe the fundamental idea comes from Alex Stepanov as his work revolves around 
        <i>regular</i> types. Such types are automatically copied, assigned and 
        compared, and so should the composite ones that they comprise. The first two points have
        been natively supported by the C++ language from the beginning and this proposal attempts
        to address the last one.

        <p>I want to thank Andrew Sutton for the early feedback and guidance, as well as
           Bjarne Stroustrup for loudly asking for consensus on small, fundamental
           language cleanups that strive to make users' lives easier.

        <p>Editorial credits go to Daniel Krügler, Ville Voutilainen, Jens Maurer 
           and Lawrence Crowl - 
           thank you for helping with the technical specification!

        <p>Finally, many folks on the <code>c++std-ext</code> list have provided valuable 
           advice and guidance. Thank you for the lively discussion and your help with 
           steering the design!

    <h2>VI. References</h2>
    <p><a name="regular">1</a> The <i>Regular</i> concept is defined by Stepanov in the 
    following way:
    <ul>
    <li>Default construction, destruction,  copy construction and assignment are defined
    <li>Copy and assignment are implemented fully (a copy is equal to the original)
    <li>Equality is defined to return <i>true</i> IFF both objects represent the same value
        (this relation is transitive, reflexive and symmetric) 
    <li>Inequality is defined as a boolean negation of equality
    </ul>

    <p><a name="totallyordered">2</a> The <i>Totally Ordered</i> concept extends <i>Regular</i>
    with the following:
    <ul>
    <li><code>operator&lt;()</code> is defined, <code>operator&gt;=()</code> 
        is defined as its boolean negation
    <li>Every relational operator is transitive
    </ul>
    <p>See <a href="http://www.elementsofprogramming.com/">"Elements of programming"</a>
       by Alexander Stepanov and Paul McJones for a full treatment of 
       <i>Regular</i> and <i>Totally Ordered</i> concepts.
</body>
</html>
