Skip to content

clang 19 raises operator<=>() return type to std::strong_ordering for double #115182

@pkl97

Description

@pkl97

This program compiles with clang 19.1.2 but fails to compile with MSVC 19.41:

#include <compare>

class C
{
    double d1;
    double d2;
    std::strong_ordering operator<=>(const C&) const = default;
};

int main()
{
    return 0;
}

The MSVC error message:

main.cpp(7): error C7549: 'C::operator <=>':
defaulted comparison function cannot be declared to return a comparison
category stronger than the common category among bases and members which was deduced to be 'std::partial_ordering'

In my opinion MSVC is correct because double by default only supports std::partial_ordering because of NaN values.

See also here: https://en.cppreference.com/w/cpp/utility/compare/partial_ordering

The built-in operator<=>between floating-point values uses this ordering:
the positive zero and the negative zero compare equivalent, but can be
distinguished, and NaN values compare unordered with any other value.

Why does clang compile the code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partydiverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions