public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105200] New: user-defined operator <=> for enumerated types is ignored
@ 2022-04-08  7:35 falbrechtskirchinger at gmail dot com
  2022-04-08 10:12 ` [Bug c++/105200] " jakub at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: falbrechtskirchinger at gmail dot com @ 2022-04-08  7:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105200
           Summary: user-defined operator <=> for enumerated types is
                    ignored
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: falbrechtskirchinger at gmail dot com
  Target Milestone: ---

Version: g++
(Compiler-Explorer-Build-gcc-45fb78c9c456ace1d914c836d15af38ae345b902-binutils-2.36.1)
12.0.1 20220407 (experimental)

Code:

#include <compare>
#include <iostream>

enum class foo { a, b, c, d };

inline std::partial_ordering operator<=>(const foo lhs, const foo rhs) {
        std::cout << "foo <=> called\n";
        return std::partial_ordering::less;
}

int main() {
        std::cout << std::boolalpha;

        bool b1 = foo::b < foo::a;
        std::cout << "foo::b < foo::a := " << b1 << "\n";

        std::cout << "=============\n";

        bool b2 = std::is_lt(foo::b <=> foo::a);
        std::cout << "std::is_lt(foo::b <=> foo::a) := " << b2 << "\n";
}

Comparison of GCC, Clang, MSVC, ICC:
https://godbolt.org/z/n7GnqhMvn


GCC (apparently) always selects the builtin operator <=> for enumerated types
and ignores the user-defined one. GCC's behavior differs from Clang, MSVC, and
ICC (although the latter, despite calling user-defined <=>, still produces the
wrong result).

Of course defining the regular comparison operators works as intended and so
this different behavior with user-defined <=> seems inconsistent, even if it
can be argued that it is to letter of the standard (see [expr.spaceship] on
comparing enumerated types). I disagree with this reading of the standard over
all.

Discussing this on IRC and referring to sections [expr.spaceship],
[over.built], [over.match.oper], and [over.match.best] of the standard no
consensus was reached as to which implementation is correct and I was asked to
open this bug report.

As a reminder to myself: Trying to find a workaround, I deleted operator <
which resulted in a segfault. I should try to re-create this ICE and file a
separate bug.

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

end of thread, other threads:[~2023-02-27 12:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-08  7:35 [Bug c++/105200] New: user-defined operator <=> for enumerated types is ignored falbrechtskirchinger at gmail dot com
2022-04-08 10:12 ` [Bug c++/105200] " jakub at gcc dot gnu.org
2022-04-08 11:31 ` jakub at gcc dot gnu.org
2022-04-08 12:26 ` falbrechtskirchinger at gmail dot com
2022-04-08 12:56 ` ppalka at gcc dot gnu.org
2022-04-13 15:35 ` ppalka at gcc dot gnu.org
2022-12-30 19:45 ` barry.revzin at gmail dot com
2023-01-01 20:00 ` jens.maurer at gmx dot net
2023-01-01 20:08 ` pinskia at gcc dot gnu.org
2023-02-27 12:26 ` jakub 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).