[clang] [clang] Reapply Handle templated operators with reversed arguments (PR #72213)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 12 02:02:30 PST 2024


================
@@ -324,6 +324,113 @@ bool x = X() == X(); // expected-warning {{ambiguous}}
 }
 } // namespace P2468R2
 
+namespace GH53954{
+namespace friend_template_1 {
+struct P {
+  template <class T>
+  friend bool operator==(const P&, const T&) { return true; } // expected-note {{candidate}} \
+                              // expected-note {{ambiguous candidate function with reversed arguments}}
+};
+struct A : public P {};
+struct B : public P {};
+bool check(A a, B b) { return a == b; } // expected-warning {{use of overloaded operator '==' (with operand types 'A' and 'B') to be ambiguous}}
+}
+
+namespace friend_template_2 {
+struct P {
+  template <class T>
+  friend bool operator==(const T&, const P&) { return true; } // expected-note {{candidate}} \
+                                              // expected-note {{ambiguous candidate function with reversed arguments}}
+};
+struct A : public P {};
+struct B : public P {};
+bool check(A a, B b) { return a == b; } // expected-warning {{use of overloaded operator '==' (with operand types 'A' and 'B') to be ambiguous}}
+}
+
+namespace friend_template_class_template {
+template<class S>
+struct P {
+  template <class T>
+  friend bool operator==(const T&, const P&) { // expected-note 2 {{candidate}}
+    return true;
+  }
+};
+struct A : public P<int> {};
+struct B : public P<bool> {};
+bool check(A a, B b) { return a == b; } // expected-warning {{ambiguous}}
+}
+
+namespace friend_template_fixme {
+// FIXME(GH70210): This should not rewrite operator== and definitely not a hard error.
----------------
ilya-biryukov wrote:

I just wanted to point out that this bug is worrying as it might break some code that's not easy to rewrite.
We worked around the problem in LLVM with 77f2ccbaac5e05e14827247ea8f6cc0f9d214390, but you could already see how the rewrite is complicated.

I don't think we should block on that, especially given that the issue you mention requires further progress from EWG and CWG. But I would keep a close eye for people complaining about this change.

https://github.com/llvm/llvm-project/pull/72213


More information about the cfe-commits mailing list