gpt4 book ai didi

c++ - 为什么可以使用 operator<=> 比较数组成员,但不能使用独立数组?

转载 作者:行者123 更新时间:2023-12-04 04:31:29 24 4
gpt4 key购买 nike

我一直在与飞船运算符(operator)一起玩,我想知道以下行为的原因是什么:

struct ArrayWrapper
{
int arr[3];
auto operator<=>(const ArrayWrapper&) const = default;
};

ArrayWrapper a1{1,2,3}, a2{1,2,4};
auto x = a1 <=> a2; // this compiles and works, x is std::strong_ordering::less
数组比较适用于成员数组。它也适用于 std::array :
std::array<int, 3> arr1{1,2,4};
std::array<int, 3> arr2{1,2,3};
auto x = arr1 <=> arr2; // x is std::strong_ordering::greater
但是,它不适用于非成员的原始数组:
int rawArr1[3]{1,2,3}, rawArr2[3]{1,2,3};
auto x = rawArr1 <=> rawArr2; // error: invalid operands of types ‘int [3]’ and ‘int [3]’ to binary ‘operator<=>’
我已经在 GCC 11 上测试过了,原因是什么?考虑到它适用于数组成员,这似乎很奇怪。

最佳答案

三路比较的目标是使(新引入的)隐式生成的比较与隐式生成的复制语义(预先存在;忽略不推荐使用的情况)一致。复制时可以观察到一致的现象:

ArrayWrapper a3          = a2;      // OK
int rawArr3[] = rawArr2; // ill-formed
最初的提案是这样说的:

P0515R0 Consistent comparison

2.2.3 Language types and operator<=>

  • For copyable arrays T[N] (i.e., that are nonstatic data members), T[N] <=> T[N] returns the same type as T’s <=> and performs lexicographical elementwise comparison. For other arrays, there is no <=> because the arrays are not copyable.

Notes ... For arrays, we don’t provide comparison if the array is not copyable in the language, to keep copying and comparison consistent. Note that for two arrays, arr1<=>arr2 is ill-formed because the array-to-pointer conversion is not applied.


以及规则的措辞(最新草案):

[expr.spaceship]

  • If at least one of the operands is of object pointer type and the other operand is of object pointer or array type, array-to-pointer conversions ([conv.array]), pointer conversions ([conv.ptr]), and qualification conversions are performed on both operands to bring them to their composite pointer type ([expr.type]).After the conversions, the operands shall have the same type.[Note 1: If both of the operands are arrays, array-to-pointer conversions are not applied.— end note]

... no other case applying to arrays

  • Otherwise, the program is ill-formed.

这就是为什么 rawArr1 <=> rawArr2不起作用。

[class.compare.default]

The direct base class subobjects of C, in the order of their declaration in the base-specifier-list of C, followed by the non-static data members of C, in the order of their declaration in the member-specification of C, 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 ([over.best.ics]), class member access expressions ([expr.ref]), and array subscript expressions ([expr.sub]) applied to x.


这就是为什么 a1 <=> a2作品。
此外,对 std::array 的保证:

[container.requirements.general]

i <=> j

Constraints: X​::​iterator meets the random access iterator requirements.

关于c++ - 为什么可以使用 operator<=> 比较数组成员,但不能使用独立数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68431126/

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