operator<=> on the C++ Standard LibraryDocument Number: P0790R1 Date: 2018-08-06 Author: David Stone (davidmstone@google.com, david@doublewise.net) Audience: LEWG, LWG
This paper lists (what are expected to be) non-controversial changes to the C++ standard library in response to P0515, which adds operator<=> 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 strong_equality or strong_ordering, matching the existing comparison operators.
This document should contain a complete list of types or categories of types in C++.
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.
The operator<=> proposal was written such that the "generated" operators are equivalent to source code rewrites – there is no actual operator== 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 a @ b 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.
operator<=> in this paperThese types are not comparable now. This paper does not propose adding any new comparisons to any of these types.
nothrow, piecewise_construct_t, etc.)plus, minus, etc.)equal_to, etc.)owner_lesslogical_and, etc.)bit_and, etc.)nested_exceptionallocator_traitschar_traitsiterator_traitsnumeric_limitspointer_traitsregex_traitschrono::duration_valuestuple_elementmax_align_tmap::node_typemap::insert_return_typeset::node_typeset::insert_return_typeunordered_map::node_typeunordered_map::insert_return_typeunordered_set::node_typeunordered_set::insert_return_typeanydefault_deletealigned_storagealigned_unionsystem_clocksteady_clockhigh_resolution_clocklocale::facetlocale::idctype_basectypectype_bynamecodecvt_basecodecvtcodecvt_bynamenum_getnum_putnumpunctnumpunct_bynamecollatecollate_bynametime_gettime_get_bynametime_puttime_put_bynamemoney_basemoney_getmoney_putmoney_punctmoneypunct_bynamemessage_basemessagesmessages_bynameFILEva_listback_insert_iteratorfront_insert_iteratorinsert_iteratorostream_iteratorostreambuf_iteratorios_baseios_base::Initbasic_iosbasic_streambufbasic_istreambasic_iostreambasic_ostreambasic_stringbufbasic_istringstreambasic_ostringstreambasic_stringstreambasic_filebufbasic_ifstreambasic_ofstreambasic_fstreamgsliceslice_arraygslice_arraymask_arrayindirect_arrayatomic_flagthreadmutexrecursive_mutextimed_mutextimed_recursive_mutexlock_guardscoped_lockunique_lockonce_flagshared_mutexshared_timed_mutexshared_lockcondition_variablecondition_variable_anypromisefutureshared_futurepackaged_taskrandom_devicehashweak_ptrbasic_regexsequential_execution_policyparallel_execution_policyparallel_vector_execution_policydefault_searcherboyer_moore_searcherboyer_moore_horspool_searcherratiointeger_sequenceseed_seq (paper needed to add strong_equality)enable_shared_from_this: It would be nice to give it a strong_ordering to allow derived classes to = default. However, this means that all classes that do not explicitly delete their comparison operator get an operator<=> that compares only the enable_shared_from_this base class, which is almost certainly wrong. Since this is intended to be used as a base class, we should not add operator<=> to it. Moreover, classes which enable_shared_from_this are unlikely to be basic value classes so they do not lose much by not being able to default.initializer_list: initializer_list 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 initializer_list containing the same set of values compared as not equal. Therefore, I recommend not defining it for this type.operator<=> in this paperdiv_tldiv_tlldiv_timaxdiv_ttimespectmlconvfenv_tfpos_tmbstate_toperator<=>, no change from current comparisonsThese types are all currently comparable.
error_category: strong_orderingerror_code: strong_orderingerror_condition: strong_orderingexception_ptr: strong_orderingtype_info: strong_equalitymonostate: strong_orderingbitset: strong_equality (paper would be needed to change this to strong_ordering)allocator: strong_equalitymemory_resource: strong_equalitysynchronized_pool_resource: (implicitly from memory_resource base class)unsynchronized_pool_resource: (implicitly from memory_resource base class)monotonic_buffer_resource: (implicitly from memory_resource base class)polymorphic_allocator: strong_equalityscoped_allocator_adaptor: strong_equalitypool_options: strong_equalityfunction: strong_equality with nullptr_t only (no homogenous operator)chrono::duration: strong_ordering, heterogeneous with durations of other representations and periodschrono::time_point: strong_ordering, heterogeneous in the durationtype_index: strong_orderinglocale: strong_equalitycomplex: strong_equality (heterogeneous with T and homogeneous)linear_congruential_engine: strong_equalitymersenne_twister_engine: strong_equalitysubtract_with_carry_engine: strong_equalitydiscard_block_engine: strong_equalityindependent_bits_engine: strong_equalityshuffle_order_engine: strong_equalityuniform_int_distribution: strong_equalityuniform_int_distribution::param_type: strong_equalityuniform_real_distribution: strong_equalityuniform_real_distribution::param_type: strong_equalitybernoulli_distribution: strong_equalitybernoulli_distribution::param_type: strong_equalitybinomial_distribution: strong_equalitybinomial_distribution::param_type: strong_equalitygeometric_distribution: strong_equalitygeometric_distribution::param_type: strong_equalitynegative_binomial_distribution: strong_equalitynegative_binomial_distribution::param_type: strong_equalitypoisson_distribution: strong_equalitypoisson_distribution::param_type: strong_equalityexponential_distribution: strong_equalityexponential_distribution::param_type: strong_equalitygamma_distribution: strong_equalitygamma_distribution::param_type: strong_equalityweibull_distribution: strong_equalityweibull_distribution::param_type: strong_equalityextreme_value_distribution: strong_equalityextreme_value_distribution::param_type: strong_equalitynormal_distribution: strong_equalitynormal_distribution::param_type: strong_equalitylognormal_distribution: strong_equalitylognormal_distribution::param_type: strong_equalitychi_squared_distribution: strong_equalitychi_squared_distribution::param_type: strong_equalitycauchy_distribution: strong_equalitycauchy_distribution::param_type: strong_equalityfisher_f_distribution: strong_equalityfisher_f_distribution::param_type: strong_equalitystudent_t_distribution: strong_equalitystudent_t_distribution::param_type: strong_equalitydiscrete_distribution: strong_equalitydiscrete_distribution::param_type: strong_equalitypiecewsie_constant_distribution: strong_equalitypiecewsie_constant_distribution::param_type: strong_equalitypiecewise_linear_distribution: strong_equalitypiecewise_linear_distribution::param_type: strong_equalityfilesystem::path: strong_orderingfilesystem::path::iterator: strong_orderingfilesystem::directory_entry: strong_orderingfilesystem::directory_iterator: strong_orderingfilesystem::recursive_directory_iterator: strong_orderingistream_iterator: strong_equalityistreambuf_iterator: strong_equalitymatch_results: strong_equalityregex_iterator: strong_equalityregex_token_iterator: strong_equalitythread::id: strong_orderingfpos: strong_equalityarray::iterator: strong_orderingdeque::iterator: strong_orderingforward_list::iterator: strong_equalitylist::iterator: strong_equalityvector::iterator: strong_orderingmap::iterator: strong_equalityset::iterator: strong_equalitymultimap::iterator: strong_equalitymultiset::iterator: strong_equalityunordered_map::iterator: strong_equalityunodered_set::iterator: strong_equalityunordered_multimap::iterator: strong_equalityunodered_multiset::iterator: strong_equalityvalarray::iterator: strong_orderingoperator<=> from a conversion operatorThese types will get operator<=> if possible without any changes, just like they already have whatever comparison operators their underlying type has.
integral_constant and all types deriving from integral_constant (has operator T)bitset::reference (has operator bool)reference_wrapper (has operator T &)atomic (has operator T)This has the disadvantage that types which have a template comparison operator will not have their wrapper convertible. For instance, std::reference_wrapper<std::string> is not currently comparable. This does not affect bitset::reference, as it has a fixed conversion to bool, but it does affect the other three.
arraydequeforward_listlistvector (including vector<bool>)mapsetmultimapmultisetunordered_mapunodered_setunodered_multimapunordered_multisetqueuequeue::iteratorpriority_queuepriority_queue::iteratorstackstack::iteratorpairtuplereverse_iteratormove_iteratoroptionalvariantThis turned out to be much more complicated than expected and will require its own paper.
basic_string, basic_string_view, char_traits, and sub_matchProperly integrating operator<=> with these types requires more thought than this paper has room for, and thus will be discussed separately.
unique_ptr and shared_ptrThey contain state that is not observed in the comparison operators. Therefore, they will get their own paper.
Current comparison operators return a valarray<bool>, giving you the result for each pair (with undefined behavior for differently-sized valarray arguments). It might make sense to provide some sort of function that returns valarray<comparison_category>, but that should not be named operator<=>. This paper does not suggest adding operator<=> to valarray.
operator<=> in another paperThis paper does not propose changing any of the following types -- they are here only for completeness.
filesystem::file_statusfilesystem::space_infosliceto_chars_resultfrom_chars_resultnullptr_tAlready supports strong_equality in the working draft. I will be writing a separate paper proposing strong_ordering.
This category includes things like BinaryPredicate and Compare. This is addressed in a separate paper.
This includes things like LessThanComparable and EqualityComparable. This is addressed in a separate paper.
All operator<=> should be constexpr and noexcept where possible, following the lead of the language feature and allowing = default as an implementation strategy for some types.
When we list a result type as "unspecified" it is unspecified whether it has operator<=>. 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.