microsoft / STL

MSVC's implementation of the C++ Standard Library.
Other
10.06k stars 1.48k forks source link

Extending memcmp optimization to classes #1591

Open AdamBucior opened 3 years ago

AdamBucior commented 3 years ago

With C++20 it's possible to default comparison operators. With has_unique_object_representations and an intrinsic to detect if type is trivially equality comparable (it has defaulted operator== and all of it's members are trivially equality comparable) we could greatly increase number of cases when equal uses memcmp.

It would be great if we could default operator== for types like tuple and pair, but I don't know if we can do that.

StephanTLavavej commented 3 years ago

I think it might be possible to default equality for pair, but not for tuple which permits different types to be compared.

CaseyCarter commented 3 years ago

I think it might be possible to default equality for pair, but not for tuple which permits different types to be compared.

pair's comparisons can't be generally defaulted since it allows reference members and "A defaulted <=> or == operator function for class C is defined as deleted if any non-static data member of C is of reference type or C has variant members." ([class.compare.default]/2).

That said, I've been thinking about implementing "has no fields of reference type" partial specializations for pair and tuple in ABI broken world so their special member functions could be defaulted. Such a partial specialization of pair could also default its comparisons if those comparisons were specified as members or hidden friends rather than vanilla non-members.