<h1 id="effect-of-operator-on-the-c-standard-library">Effect of <code>operator&lt;=&gt;</code> on the C++ Standard Library</h1>
<pre>
Document Number: P0790R1
Date: 2018-08-06
Author: David Stone (&#100;&#97;&#118;&#105;&#100;&#109;&#115;&#116;&#111;&#110;&#101;&#64;&#103;&#111;&#111;&#103;&#108;&#101;&#46;&#99;&#111;&#109;, &#100;&#97;&#118;&#105;&#100;&#64;&#100;&#111;&#117;&#98;&#108;&#101;&#119;&#105;&#115;&#101;&#46;&#110;&#101;&#116;)
Audience: LEWG, LWG
</pre>

<p>This paper lists (what are expected to be) non-controversial changes to the C++ standard library in response to <a href="https://wg21.link/P0515">P0515</a>, which adds <code>operator&lt;=&gt;</code> to the language. This is expected to be non-controversial because it tries to match existing behavior as much as possible. As a result, all proposed additions are either <code>strong_equality</code> or <code>strong_ordering</code>, matching the existing comparison operators.</p>
<p>This document should contain a complete list of types or categories of types in C++.</p>
<h2 id="revision-history">Revision History</h2>
<p>R1: A much broader version of this paper was presented to LEWG at a previous meeting. What remains in this paper is everything which the group did not find controversial and which probably does not require significant justification. All controversial aspects will be submitted in separate papers.</p>
<h2 id="backward-compatibility">Backward Compatibility</h2>
<p>The <code>operator&lt;=&gt;</code> proposal was written such that the &quot;generated&quot; operators are equivalent to source code rewrites – there is no actual <code>operator==</code> that a user could take the address of. Users are not allowed to form pointers to standard library member functions and are unable to form pointers to friend functions defined inline in the class. There are some cases where we do not specify how the operator was implemented, only that the expression <code>a @ b</code> is valid; these cases are not broken by such a change because users could not have depended on it, anyway. In general, we accept changes that overload existing functions, which also has the effect of breaking code which takes the address of a free function.</p>
<h2 id="types-that-are-not-proposed-to-get-operator-in-this-paper">Types that are not proposed to get <code>operator&lt;=&gt;</code> in this paper</h2>
<p>These types are not comparable now. This paper does not propose adding any new comparisons to any of these types.</p>
<ul>
<li>deprecated types</li>
<li>exception types</li>
<li>tag classes (<code>nothrow</code>, <code>piecewise_construct_t</code>, etc.)</li>
<li>arithmetic function objects (<code>plus</code>, <code>minus</code>, etc.)</li>
<li>comparison function objects (<code>equal_to</code>, etc.)</li>
<li><code>owner_less</code></li>
<li>logical function objects (<code>logical_and</code>, etc.)</li>
<li>bitwise function objects (<code>bit_and</code>, etc.)</li>
<li><code>nested_exception</code></li>
<li><code>allocator_traits</code></li>
<li><code>char_traits</code></li>
<li><code>iterator_traits</code></li>
<li><code>numeric_limits</code></li>
<li><code>pointer_traits</code></li>
<li><code>regex_traits</code></li>
<li><code>chrono::duration_values</code></li>
<li><code>tuple_element</code></li>
<li><code>max_align_t</code></li>
<li><code>map::node_type</code></li>
<li><code>map::insert_return_type</code></li>
<li><code>set::node_type</code></li>
<li><code>set::insert_return_type</code></li>
<li><code>unordered_map::node_type</code></li>
<li><code>unordered_map::insert_return_type</code></li>
<li><code>unordered_set::node_type</code></li>
<li><code>unordered_set::insert_return_type</code></li>
<li><code>any</code></li>
<li><code>default_delete</code></li>
<li><code>aligned_storage</code></li>
<li><code>aligned_union</code></li>
<li><code>system_clock</code></li>
<li><code>steady_clock</code></li>
<li><code>high_resolution_clock</code></li>
<li><code>locale::facet</code></li>
<li><code>locale::id</code></li>
<li><code>ctype_base</code></li>
<li><code>ctype</code></li>
<li><code>ctype_byname</code></li>
<li><code>codecvt_base</code></li>
<li><code>codecvt</code></li>
<li><code>codecvt_byname</code></li>
<li><code>num_get</code></li>
<li><code>num_put</code></li>
<li><code>numpunct</code></li>
<li><code>numpunct_byname</code></li>
<li><code>collate</code></li>
<li><code>collate_byname</code></li>
<li><code>time_get</code></li>
<li><code>time_get_byname</code></li>
<li><code>time_put</code></li>
<li><code>time_put_byname</code></li>
<li><code>money_base</code></li>
<li><code>money_get</code></li>
<li><code>money_put</code></li>
<li><code>money_punct</code></li>
<li><code>moneypunct_byname</code></li>
<li><code>message_base</code></li>
<li><code>messages</code></li>
<li><code>messages_byname</code></li>
<li><code>FILE</code></li>
<li><code>va_list</code></li>
<li><code>back_insert_iterator</code></li>
<li><code>front_insert_iterator</code></li>
<li><code>insert_iterator</code></li>
<li><code>ostream_iterator</code></li>
<li><code>ostreambuf_iterator</code></li>
<li><code>ios_base</code></li>
<li><code>ios_base::Init</code></li>
<li><code>basic_ios</code></li>
<li><code>basic_streambuf</code></li>
<li><code>basic_istream</code></li>
<li><code>basic_iostream</code></li>
<li><code>basic_ostream</code></li>
<li><code>basic_stringbuf</code></li>
<li><code>basic_istringstream</code></li>
<li><code>basic_ostringstream</code></li>
<li><code>basic_stringstream</code></li>
<li><code>basic_filebuf</code></li>
<li><code>basic_ifstream</code></li>
<li><code>basic_ofstream</code></li>
<li><code>basic_fstream</code></li>
<li><code>gslice</code></li>
<li><code>slice_array</code></li>
<li><code>gslice_array</code></li>
<li><code>mask_array</code></li>
<li><code>indirect_array</code></li>
<li><code>atomic_flag</code></li>
<li><code>thread</code></li>
<li><code>mutex</code></li>
<li><code>recursive_mutex</code></li>
<li><code>timed_mutex</code></li>
<li><code>timed_recursive_mutex</code></li>
<li><code>lock_guard</code></li>
<li><code>scoped_lock</code></li>
<li><code>unique_lock</code></li>
<li><code>once_flag</code></li>
<li><code>shared_mutex</code></li>
<li><code>shared_timed_mutex</code></li>
<li><code>shared_lock</code></li>
<li><code>condition_variable</code></li>
<li><code>condition_variable_any</code></li>
<li><code>promise</code></li>
<li><code>future</code></li>
<li><code>shared_future</code></li>
<li><code>packaged_task</code></li>
<li><code>random_device</code></li>
<li><code>hash</code></li>
<li><code>weak_ptr</code></li>
<li><code>basic_regex</code></li>
<li><code>sequential_execution_policy</code></li>
<li><code>parallel_execution_policy</code></li>
<li><code>parallel_vector_execution_policy</code></li>
<li><code>default_searcher</code></li>
<li><code>boyer_moore_searcher</code></li>
<li><code>boyer_moore_horspool_searcher</code></li>
<li><code>ratio</code></li>
<li><code>integer_sequence</code></li>
<li><code>seed_seq</code> (paper needed to add <code>strong_equality</code>)</li>
<li><code>enable_shared_from_this</code>: It would be nice to give it a <code>strong_ordering</code> to allow derived classes to <code>= default</code>. However, this means that all classes that do not explicitly delete their comparison operator get an <code>operator&lt;=&gt;</code> that compares only the <code>enable_shared_from_this</code> base class, which is almost certainly wrong. Since this is intended to be used as a base class, we should not add <code>operator&lt;=&gt;</code> to it. Moreover, classes which <code>enable_shared_from_this</code> are unlikely to be basic value classes so they do not lose much by not being able to default.</li>
<li><code>initializer_list</code>: <code>initializer_list</code> is a reference type. It would be strange to give it reference semantics on copy but value semantics for comparison. It would also be surprising if two <code>initializer_list</code> containing the same set of values compared as not equal. Therefore, I recommend not defining it for this type.</li>
</ul>
<h3 id="types-from-c-that-are-not-proposed-to-get-operator-in-this-paper">Types from C that are not proposed to get <code>operator&lt;=&gt;</code> in this paper</h3>
<ul>
<li><code>div_t</code></li>
<li><code>ldiv_t</code></li>
<li><code>lldiv_t</code></li>
<li><code>imaxdiv_t</code></li>
<li><code>timespec</code></li>
<li><code>tm</code></li>
<li><code>lconv</code></li>
<li><code>fenv_t</code></li>
<li><code>fpos_t</code></li>
<li><code>mbstate_t</code></li>
</ul>
<h2 id="types-that-should-get-operator-no-change-from-current-comparisons">Types that should get <code>operator&lt;=&gt;</code>, no change from current comparisons</h2>
<p>These types are all currently comparable.</p>
<ul>
<li><code>error_category</code>: <code>strong_ordering</code></li>
<li><code>error_code</code>: <code>strong_ordering</code></li>
<li><code>error_condition</code>: <code>strong_ordering</code></li>
<li><code>exception_ptr</code>: <code>strong_ordering</code></li>
<li><code>type_info</code>: <code>strong_equality</code></li>
<li><code>monostate</code>: <code>strong_ordering</code></li>
<li><code>bitset</code>: <code>strong_equality</code> (paper would be needed to change this to <code>strong_ordering</code>)</li>
<li><code>allocator</code>: <code>strong_equality</code></li>
<li><code>memory_resource</code>: <code>strong_equality</code></li>
<li><code>synchronized_pool_resource</code>: (implicitly from <code>memory_resource</code> base class)</li>
<li><code>unsynchronized_pool_resource</code>: (implicitly from <code>memory_resource</code> base class)</li>
<li><code>monotonic_buffer_resource</code>: (implicitly from <code>memory_resource</code> base class)</li>
<li><code>polymorphic_allocator</code>: <code>strong_equality</code></li>
<li><code>scoped_allocator_adaptor</code>: <code>strong_equality</code></li>
<li><code>pool_options</code>: <code>strong_equality</code></li>
<li><code>function</code>: <code>strong_equality</code> with <code>nullptr_t</code> only (no homogenous operator)</li>
<li><code>chrono::duration</code>: <code>strong_ordering</code>, heterogeneous with durations of other representations and periods</li>
<li><code>chrono::time_point</code>: <code>strong_ordering</code>, heterogeneous in the duration</li>
<li><code>type_index</code>: <code>strong_ordering</code></li>
<li><code>locale</code>: <code>strong_equality</code></li>
<li><code>complex</code>: <code>strong_equality</code> (heterogeneous with <code>T</code> and homogeneous)</li>
<li><code>linear_congruential_engine</code>: <code>strong_equality</code></li>
<li><code>mersenne_twister_engine</code>: <code>strong_equality</code></li>
<li><code>subtract_with_carry_engine</code>: <code>strong_equality</code></li>
<li><code>discard_block_engine</code>: <code>strong_equality</code></li>
<li><code>independent_bits_engine</code>: <code>strong_equality</code></li>
<li><code>shuffle_order_engine</code>: <code>strong_equality</code></li>
<li><code>uniform_int_distribution</code>: <code>strong_equality</code></li>
<li><code>uniform_int_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>uniform_real_distribution</code>: <code>strong_equality</code></li>
<li><code>uniform_real_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>bernoulli_distribution</code>: <code>strong_equality</code></li>
<li><code>bernoulli_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>binomial_distribution</code>: <code>strong_equality</code></li>
<li><code>binomial_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>geometric_distribution</code>: <code>strong_equality</code></li>
<li><code>geometric_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>negative_binomial_distribution</code>: <code>strong_equality</code></li>
<li><code>negative_binomial_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>poisson_distribution</code>: <code>strong_equality</code></li>
<li><code>poisson_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>exponential_distribution</code>: <code>strong_equality</code></li>
<li><code>exponential_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>gamma_distribution</code>: <code>strong_equality</code></li>
<li><code>gamma_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>weibull_distribution</code>: <code>strong_equality</code></li>
<li><code>weibull_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>extreme_value_distribution</code>: <code>strong_equality</code></li>
<li><code>extreme_value_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>normal_distribution</code>: <code>strong_equality</code></li>
<li><code>normal_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>lognormal_distribution</code>: <code>strong_equality</code></li>
<li><code>lognormal_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>chi_squared_distribution</code>: <code>strong_equality</code></li>
<li><code>chi_squared_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>cauchy_distribution</code>: <code>strong_equality</code></li>
<li><code>cauchy_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>fisher_f_distribution</code>: <code>strong_equality</code></li>
<li><code>fisher_f_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>student_t_distribution</code>: <code>strong_equality</code></li>
<li><code>student_t_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>discrete_distribution</code>: <code>strong_equality</code></li>
<li><code>discrete_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>piecewsie_constant_distribution</code>: <code>strong_equality</code></li>
<li><code>piecewsie_constant_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>piecewise_linear_distribution</code>: <code>strong_equality</code></li>
<li><code>piecewise_linear_distribution::param_type</code>: <code>strong_equality</code></li>
<li><code>filesystem::path</code>: <code>strong_ordering</code></li>
<li><code>filesystem::path::iterator</code>: <code>strong_ordering</code></li>
<li><code>filesystem::directory_entry</code>: <code>strong_ordering</code></li>
<li><code>filesystem::directory_iterator</code>: <code>strong_ordering</code></li>
<li><code>filesystem::recursive_directory_iterator</code>: <code>strong_ordering</code></li>
<li><code>istream_iterator</code>: <code>strong_equality</code></li>
<li><code>istreambuf_iterator</code>: <code>strong_equality</code></li>
<li><code>match_results</code>: <code>strong_equality</code></li>
<li><code>regex_iterator</code>: <code>strong_equality</code></li>
<li><code>regex_token_iterator</code>: <code>strong_equality</code></li>
<li><code>thread::id</code>: <code>strong_ordering</code></li>
<li><code>fpos</code>: <code>strong_equality</code></li>
<li><code>array::iterator</code>: <code>strong_ordering</code></li>
<li><code>deque::iterator</code>: <code>strong_ordering</code></li>
<li><code>forward_list::iterator</code>: <code>strong_equality</code></li>
<li><code>list::iterator</code>: <code>strong_equality</code></li>
<li><code>vector::iterator</code>: <code>strong_ordering</code></li>
<li><code>map::iterator</code>: <code>strong_equality</code></li>
<li><code>set::iterator</code>: <code>strong_equality</code></li>
<li><code>multimap::iterator</code>: <code>strong_equality</code></li>
<li><code>multiset::iterator</code>: <code>strong_equality</code></li>
<li><code>unordered_map::iterator</code>: <code>strong_equality</code></li>
<li><code>unodered_set::iterator</code>: <code>strong_equality</code></li>
<li><code>unordered_multimap::iterator</code>: <code>strong_equality</code></li>
<li><code>unodered_multiset::iterator</code>: <code>strong_equality</code></li>
<li><code>valarray::iterator</code>: <code>strong_ordering</code></li>
</ul>
<h2 id="types-that-will-get-their-operator-from-a-conversion-operator">Types that will get their <code>operator&lt;=&gt;</code> from a conversion operator</h2>
<p>These types will get <code>operator&lt;=&gt;</code> if possible without any changes, just like they already have whatever comparison operators their underlying type has.</p>
<ul>
<li><code>integral_constant</code> and all types deriving from <code>integral_constant</code> (has <code>operator T</code>)</li>
<li><code>bitset::reference</code> (has <code>operator bool</code>)</li>
<li><code>reference_wrapper</code> (has <code>operator T &amp;</code>)</li>
<li><code>atomic</code> (has <code>operator T</code>)</li>
</ul>
<p>This has the disadvantage that types which have a template comparison operator will not have their wrapper convertible. For instance, <code>std::reference_wrapper&lt;std::string&gt;</code> is not currently comparable. This does not affect <code>bitset::reference</code>, as it has a fixed conversion to <code>bool</code>, but it does affect the other three.</p>
<h2 id="types-that-wrap-another-type">Types that wrap another type</h2>
<ul>
<li><code>array</code></li>
<li><code>deque</code></li>
<li><code>forward_list</code></li>
<li><code>list</code></li>
<li><code>vector</code> (including <code>vector&lt;bool&gt;</code>)</li>
<li><code>map</code></li>
<li><code>set</code></li>
<li><code>multimap</code></li>
<li><code>multiset</code></li>
<li><code>unordered_map</code></li>
<li><code>unodered_set</code></li>
<li><code>unodered_multimap</code></li>
<li><code>unordered_multiset</code></li>
<li><code>queue</code></li>
<li><code>queue::iterator</code></li>
<li><code>priority_queue</code></li>
<li><code>priority_queue::iterator</code></li>
<li><code>stack</code></li>
<li><code>stack::iterator</code></li>
<li><code>pair</code></li>
<li><code>tuple</code></li>
<li><code>reverse_iterator</code></li>
<li><code>move_iterator</code></li>
<li><code>optional</code></li>
<li><code>variant</code></li>
</ul>
<p>This turned out to be much more complicated than expected and will require its own paper.</p>
<h3 id="-basic_string-basic_string_view-char_traits-and-sub_match-"><code>basic_string</code>, <code>basic_string_view</code>, <code>char_traits</code>, and <code>sub_match</code></h3>
<p>Properly integrating <code>operator&lt;=&gt;</code> with these types requires more thought than this paper has room for, and thus will be discussed separately.</p>
<h3 id="-unique_ptr-and-shared_ptr-"><code>unique_ptr</code> and <code>shared_ptr</code></h3>
<p>They contain state that is not observed in the comparison operators. Therefore, they will get their own paper.</p>
<h3 id="valarray">valarray</h3>
<p>Current comparison operators return a <code>valarray&lt;bool&gt;</code>, giving you the result for each pair (with undefined behavior for differently-sized <code>valarray</code> arguments). It might make sense to provide some sort of function that returns <code>valarray&lt;comparison_category&gt;</code>, but that should not be named <code>operator&lt;=&gt;</code>. This paper does not suggest adding <code>operator&lt;=&gt;</code> to <code>valarray</code>.</p>
<h2 id="types-that-have-no-comparisons-now-but-are-being-proposed-to-get-operator-in-another-paper">Types that have no comparisons now but are being proposed to get <code>operator&lt;=&gt;</code> in another paper</h2>
<p>This paper does not propose changing any of the following types -- they are here only for completeness.</p>
<ul>
<li><code>filesystem::file_status</code></li>
<li><code>filesystem::space_info</code></li>
<li><code>slice</code></li>
<li><code>to_chars_result</code></li>
<li><code>from_chars_result</code></li>
</ul>
<h2 id="-nullptr_t-"><code>nullptr_t</code></h2>
<p>Already supports <code>strong_equality</code> in the working draft. I will be writing a separate paper proposing <code>strong_ordering</code>.</p>
<h2 id="not-updating-concepts-that-provide-comparisons">Not Updating Concepts That Provide Comparisons</h2>
<p>This category includes things like <code>BinaryPredicate</code> and <code>Compare</code>. This is addressed in a separate paper.</p>
<h2 id="not-updating-concepts-that-require-comparisons">Not Updating Concepts That Require Comparisons</h2>
<p>This includes things like <code>LessThanComparable</code> and <code>EqualityComparable</code>. This is addressed in a separate paper.</p>
<h2 id="miscellaneous">Miscellaneous</h2>
<p>All <code>operator&lt;=&gt;</code> should be <code>constexpr</code> and <code>noexcept</code> where possible, following the lead of the language feature and allowing <code>= default</code> as an implementation strategy for some types.</p>
<p>When we list a result type as &quot;unspecified&quot; it is unspecified whether it has <code>operator&lt;=&gt;</code>. There are not any unspecified result types for which we currently guarantee any comparison operators are present, so there is no extra work to do here.</p>

