Bug 94924 - Default equality operator for C-array compares addresses, not data
Summary: Default equality operator for C-array compares addresses, not data
Status: RESOLVED DUPLICATE of bug 93480
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: 24666
  Show dependency treegraph
 
Reported: 2020-05-02 14:12 UTC by rhalbersma
Modified: 2022-12-26 15:50 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-05-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rhalbersma 2020-05-02 14:12:14 UTC
It appears that the default op== for a C-array has array-to-pointer decay applied and does not compare the underlying array data.

struct S {
    int data[1];
    bool operator==(const S&) const = default;
};

int main()
{
    static_assert(S{1} != S{1});
    constexpr auto s = S{1};
    static_assert(s == s);
}

Tested on tip-of-trunk at Wandbox:
https://wandbox.org/permlink/YxUv5dFs7mSGlNBx

Works correctly on Clang >= 10.
Comment 1 rhalbersma 2020-05-12 11:49:02 UTC
I just became aware of [depr.array.comp] in N4861

"Equality and relational comparisons (7.6.10, 7.6.9) between two operands of array type are deprecated."

This is very surprising to say the least and disqualifies default op= for any class containing a C-array. 

But in any case, should such code generate a warning message? And this bug be closed?
Comment 2 rhalbersma 2020-05-12 12:08:00 UTC
OK, Alisdair Meredith points out on Twitter that in [class.compare.default]/6 there is a special sentence on array class data members

In that list, any subobject of array type is recursively expanded to the sequence of its elements, in the order of increasing subscript.

So only direct array comparisons are deprecated.
Comment 3 Patrick Palka 2021-04-22 19:32:06 UTC
Looks like a dup of PR93480, which has been fixed for GCC 11.

*** This bug has been marked as a duplicate of bug 93480 ***
Comment 4 rhalbersma 2022-12-26 15:50:36 UTC
Is there any chance that this bug fix can be backported to gcc 10 also?