Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should operator<=> synthesize array comparisons?

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?

like image 890
Tybalt Avatar asked Sep 30 '20 14:09

Tybalt


1 Answers

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.

like image 65
eerorika Avatar answered Oct 31 '22 15:10

eerorika