operator<=> 应该综合数组比较吗?
Should operator<=> synthesize array comparisons?
我一直在玩我自己的 std::array 实现并注意到 libc++ 的版本为每个比较使用明确定义的运算符 (==,!=,<,>,<=,>=) .我想我可以通过实现 C++20 的宇宙飞船运算符 (<=>) 来简化我的代码。但是,当我在结构体中用 auto operator<=>(const Array<TYPE,SIZE>&) const = default;
替换非成员比较运算符时,GCC trunk 指示该函数“被隐式删除,因为默认定义的格式不正确”。一些调查表明原始数组成员是罪魁祸首。
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 表示只有copyable数组参与比较合成
出于好奇,我 运行 编译器资源管理器上第一个 link 的代码。 It also fails to compile on gcc trunk. However, clang trunk compiles the code successfully.
所以,我的问题是:哪个编译器是正确的?是否应该为成员数组合成比较?
Should operator<=> synthesize array comparisons?
是的。标准(最终工作草案)是这样说的:
[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.
我一直在玩我自己的 std::array 实现并注意到 libc++ 的版本为每个比较使用明确定义的运算符 (==,!=,<,>,<=,>=) .我想我可以通过实现 C++20 的宇宙飞船运算符 (<=>) 来简化我的代码。但是,当我在结构体中用 auto operator<=>(const Array<TYPE,SIZE>&) const = default;
替换非成员比较运算符时,GCC trunk 指示该函数“被隐式删除,因为默认定义的格式不正确”。一些调查表明原始数组成员是罪魁祸首。
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
出于好奇,我 运行 编译器资源管理器上第一个 link 的代码。 It also fails to compile on gcc trunk. However, clang trunk compiles the code successfully.
所以,我的问题是:哪个编译器是正确的?是否应该为成员数组合成比较?
Should operator<=> synthesize array comparisons?
是的。标准(最终工作草案)是这样说的:
[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.