[llvm-bugs] [Bug 47007] New: three way comparison not able to synthesize traditional comparators when explicitly defined

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Aug 5 14:24:22 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47007

            Bug ID: 47007
           Summary: three way comparison not able to synthesize
                    traditional comparators when explicitly defined
           Product: clang
           Version: 10.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: gumby at henkel-wallace.org
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

The following code fails to synthesize traditional comparators from the
three-way comparator.  `= default` works in this particular case.

As a side point: Per my reading of the standard: I don't believe <compare>
should be required for the three way case (not required for the == case).  I
wasn't sure if I should file this as a separate libc++ bug or if it's part of
this same issue.

#include <compare>
#include <iostream>

struct foo {
    int content;
    foo(int x) : content { x } {}

  // works: auto operator <=> (foo const& rhs) const = default;
  // works: auto operator ==  (foo const& rhs) const { return content == 
rhs.content; }
  // fails: 
            auto operator <=> (foo const& rhs) const { return content <=>
rhs.content; }
};

int main() {
  foo a { 1 };
  foo b { 2 };
  foo c { 1 };

  if (a != b) std::cout << "not ";
  std::cout  << "equal" << std::endl;


  if (!(a == c)) std::cout << "not ";
  std::cout  << "equal" << std::endl;

  return 0;
}


$ /usr/local/opt/llvm/bin/clang++ -std=c++20 foo.cc && ./a.out
foo.cc:24:9: error: invalid operands to binary expression ('foo' and 'foo')
  if (a != b) std::cout << "not ";
      ~ ^  ~
foo.cc:28:11: error: invalid operands to binary expression ('foo' and 'foo')
  if (!(a == c)) std::cout << "not ";
        ~ ^  ~

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200805/69543369/attachment.html>


More information about the llvm-bugs mailing list