public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/114153] New: std::less prefers operator const void*() over operator<=>() in C++20 mode
@ 2024-02-28 14:19 marc.mutz at hotmail dot com
  2024-02-28 14:26 ` [Bug libstdc++/114153] std::less<> " marc.mutz at hotmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: marc.mutz at hotmail dot com @ 2024-02-28 14:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153

            Bug ID: 114153
           Summary: std::less prefers operator const void*() over
                    operator<=>() in C++20 mode
           Product: gcc
           Version: 12.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marc.mutz at hotmail dot com
  Target Milestone: ---

std::less (and other related types like std::greater_equal, etc) is implemented
in the following way:
* if `operator<(T, U)` is defined for the argument types, it is called.
* otherwise, if the argument types are convertible to `const volatile void *`,
such conversion is performed, and it boils down to comparing the pointers.

Now, assume a type which has an `operator const void *() const`, and provides
`operator==()` and `operator<=>()` to generate all relational operators, the
same way the std types do.

So std::less will not use `operator<=>()`, but cast to `const void *` and
compare pointers. 
This is wrong, because `operator<=>()` implies all relational operators, so it
can be used to do the proper comparison. libc++ gets this right:

// https://godbolt.org/z/E55eeosP9
// Courtesy of Ivan Solovev <ivan.solovev@qt.io>
#include <compare>
#include <functional>
#include <iostream>

struct S
{
    int val;

    S(int v) : val(v) {}

    operator const void *() const { std::cout << "cast\n"; return &val; }

    friend bool operator==(S lhs, S rhs) noexcept 
    { std::cout << "op==\n"; return lhs.val == rhs.val; }
    friend std::strong_ordering operator<=>(S lhs, S rhs) noexcept 
    { std::cout << "op<=>\n"; return lhs.val <=> rhs.val; }  
};

int main()
{
    const S arr[] = {S{2}, S{1}};
    // In C++20 mode it compares pointers, and so considers that arr[1] >
arr[0],
    // which is wrong!
    return std::greater_equal<>{}(arr[0], arr[1]) ? 0 : 1;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/114153] std::less<> prefers operator const void*() over operator<=>() in C++20 mode
  2024-02-28 14:19 [Bug libstdc++/114153] New: std::less prefers operator const void*() over operator<=>() in C++20 mode marc.mutz at hotmail dot com
@ 2024-02-28 14:26 ` marc.mutz at hotmail dot com
  2024-02-28 18:28 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: marc.mutz at hotmail dot com @ 2024-02-28 14:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153

--- Comment #1 from Marc Mutz <marc.mutz at hotmail dot com> ---
It's only the C++14 "diamond"/is_transparent version of std::less/greater_equal
that is affected. If you replace the return from main with greater_equal<S>{},
then it calls op<=>, too:

// https://godbolt.org/z/cnjssh3ss
    return std::greater_equal<S>{}(arr[0], arr[1]) ? 0 : 1;
    //                        ^ added

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/114153] std::less<> prefers operator const void*() over operator<=>() in C++20 mode
  2024-02-28 14:19 [Bug libstdc++/114153] New: std::less prefers operator const void*() over operator<=>() in C++20 mode marc.mutz at hotmail dot com
  2024-02-28 14:26 ` [Bug libstdc++/114153] std::less<> " marc.mutz at hotmail dot com
@ 2024-02-28 18:28 ` redi at gcc dot gnu.org
  2024-02-28 18:29 ` redi at gcc dot gnu.org
  2024-02-29 17:40 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-28 18:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Without looking at the code, we probably just need to check if the type has a
usable operator<=>.

We check it out had a usable operator< which worked fine in C++17, but in C++20
x<y can work without having any operator<

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/114153] std::less<> prefers operator const void*() over operator<=>() in C++20 mode
  2024-02-28 14:19 [Bug libstdc++/114153] New: std::less prefers operator const void*() over operator<=>() in C++20 mode marc.mutz at hotmail dot com
  2024-02-28 14:26 ` [Bug libstdc++/114153] std::less<> " marc.mutz at hotmail dot com
  2024-02-28 18:28 ` redi at gcc dot gnu.org
@ 2024-02-28 18:29 ` redi at gcc dot gnu.org
  2024-02-29 17:40 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-28 18:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #2)
> Without looking at the code, we probably just need to check if the type has
> a usable operator<=>.
> 
> We check it out had


Sorry, phone typos. 

"We check if it has..."

> a usable operator< which worked fine in C++17, but in
> C++20 x<y can work without having any operator<

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug libstdc++/114153] std::less<> prefers operator const void*() over operator<=>() in C++20 mode
  2024-02-28 14:19 [Bug libstdc++/114153] New: std::less prefers operator const void*() over operator<=>() in C++20 mode marc.mutz at hotmail dot com
                   ` (2 preceding siblings ...)
  2024-02-28 18:29 ` redi at gcc dot gnu.org
@ 2024-02-29 17:40 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-29 17:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114153

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Created attachment 57577
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57577&action=edit
Check for <=>

Something like this (but it's also needed for std::greater and std::less_equal)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-29 17:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-28 14:19 [Bug libstdc++/114153] New: std::less prefers operator const void*() over operator<=>() in C++20 mode marc.mutz at hotmail dot com
2024-02-28 14:26 ` [Bug libstdc++/114153] std::less<> " marc.mutz at hotmail dot com
2024-02-28 18:28 ` redi at gcc dot gnu.org
2024-02-28 18:29 ` redi at gcc dot gnu.org
2024-02-29 17:40 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).