I've been playing with my own implementation of std::array and noticed libc++'s version uses explicitly defined operators for each of the comparisons (==,!=,<,>,<=,>=). I figured I could simplify my code by implementing C++20's spaceship operator (<=>). However, when I replaced the non-member comparison operators with auto operator<=>(const Array<TYPE,SIZE>&) const = default;
in the struct body, GCC trunk indicated that the function was "implicitly deleted because the default definition would be ill-formed". Some investigation indicated that the raw array member was the culprit.
This webpage indicates that, "The compiler knows how to expand members of classes that are arrays into their lists of sub-objects and compare them recursively." And this SO answer indicates that only copyable arrays participate in the comparison synthesis.
Out of curiousity, I ran the code from the first link on Compiler Explorer. It also fails to compile on gcc trunk. However, clang trunk compiles the code successfully.
So, my question is: which compiler is right? Should the comparison be synthesized for member arrays or not?
Should operator<=> synthesize array comparisons?
Yes. This is what the standard (final working draft) says:
[class.compare.default]
A defaulted comparison operator function (12.6.2) for some class C shall be a ...
The direct base class subobjects of C ... followed by the non-static data members of C, in the order of their declaration in the member-specification of form a list of subobjects. In that list, any subobject of array type is recursively expanded to the sequence of its elements, in the order of increasing subscript. Let xi be an lvalue denoting the ith element in the expanded list of subobjects for an object x(of length n), where xi is formed by a sequence of derived-to-base conversions (12.4.3.1), class member access expressions (7.6.1.4), and array subscript expressions (7.6.1.1) applied to x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With