gpt4 book ai didi

c++ - operator<=> 是否应该合成数组比较?

转载 作者:行者123 更新时间:2023-12-03 06:50:18 25 4
gpt4 key购买 nike

我一直在玩我自己的 std::array 实现,并注意到 libc++ 的版本对每个比较(==、!=、<、>、<=、>=)使用明确定义的运算符。我想我可以通过实现 C++20 的飞船操作符 (<=>) 来简化我的代码。但是,当我用 auto operator<=>(const Array<TYPE,SIZE>&) const = default; 替换非成员比较运算符时在结构体中,GCC trunk 表示该函数“被隐式删除,因为默认定义格式错误”。一些调查表明原始数组成员是罪魁祸首。
This webpage表示,“编译器知道如何将数组类的成员扩展到它们的子对象列表中,并递归地比较它们。”和 this SO answer表示只有可复制的数组参与比较综合。
出于好奇,我从编译器资源管理器上的第一个链接运行代码。 It also fails to compile on gcc trunk .然而,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.

关于c++ - operator<=> 是否应该合成数组比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64139903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com