c++: Add test for c++/93807
authorMarek Polacek <polacek@redhat.com>
Tue, 21 Apr 2020 22:11:33 +0000 (18:11 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 22 Apr 2020 19:53:34 +0000 (15:53 -0400)
This PR was initially accepts-invalid, but I think it's actually valid
C++20 code.  My reasoning is that in C++20 we no longer require the
declaration of operator== (#if-defed in the test), because C++20's
[temp.names]/2 says "A name is also considered to refer to a template
if it is an unqualified-id followed by a < and name lookup either finds
one or more functions or finds nothing." so when we're parsing

  constexpr friend bool operator==<T>(T lhs, const Foo& rhs);

we treat "operator==" as a template name, because name lookup of
"operator==" found nothing and we have an operator-function-id, which is
an unqualified-id, and it's followed by a <.  So the declaration isn't
needed to treat "operator==<T>" as a template-id.

PR c++/93807
* g++.dg/cpp2a/fn-template20.C: New test.

gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp2a/fn-template20.C [new file with mode: 0644]

index 72cc766..955418f 100644 (file)
@@ -1,3 +1,8 @@
+2020-04-22  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/93807
+       * g++.dg/cpp2a/fn-template20.C: New test.
+
 2020-04-22  Duan bo  <duanbo3@huawei.com>
 
        PR testsuite/94712
diff --git a/gcc/testsuite/g++.dg/cpp2a/fn-template20.C b/gcc/testsuite/g++.dg/cpp2a/fn-template20.C
new file mode 100644 (file)
index 0000000..c558ad1
--- /dev/null
@@ -0,0 +1,34 @@
+// PR c++/93807
+// { dg-do compile { target c++11 } }
+
+// In C++17, we need the following declaration to treat operator== as
+// a template name.  In C++20, this is handled by [temp.names]/2.
+#if __cplusplus <= 201703L
+template <typename T>
+class Foo;
+template <typename T>
+constexpr bool operator==(T lhs, const Foo<T>& rhs);
+#endif
+
+template <typename T>
+class Foo {
+public:
+  constexpr Foo(T k) : mK(k) {}
+
+  constexpr friend bool operator==<T>(T lhs, const Foo& rhs);
+private:
+  T mK;
+};
+
+template <typename T>
+constexpr bool
+operator==(T lhs, const Foo<T>& rhs)
+{
+  return lhs == rhs.mK;
+}
+
+int
+main ()
+{
+  return 1 == Foo<int>(1) ? 0 : 1;
+}