[clang] [Clang] Implement CWG2918 'Consideration of constraints for address of overloaded function' (PR #127773)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 20 04:52:02 PST 2025


================
@@ -60,6 +60,92 @@ struct S {
 #endif
 } // namespace cwg2917
 
+namespace cwg2918 { // cwg2918: 21
+
+#if __cplusplus >= 202002L
+
+namespace Example1 {
+
+template<bool B> struct X {
+  void f(short) requires B;
+  void f(long);
+  template<typename> void g(short) requires B;
+  template<typename> void g(long);
+};
+
+void test() {
+  &X<true>::f;      // since-cxx20-error {{reference to overloaded function could not be resolved}}
+  &X<true>::g<int>; // since-cxx20-error {{reference to overloaded function could not be resolved}}
+}
+
+} // namespace Example1
+
+namespace Example2 {
+
+template <bool B> struct X {
+  static constexpr int f(short) requires B {
+    return 42;
+  }
+  static constexpr int f(short) {
+    return 24;
+  }
+};
+
+template <typename T>
+constexpr int f(T) { return 1; }
+
+template <typename T>
+  requires __is_same(T, int)
+constexpr int f(T) { return 2; }
+
+void test() {
+  constexpr auto x = &X<true>::f;
+  static_assert(__is_same(decltype(x), int(*const)(short)), "");
+  static_assert(x(0) == 42, "");
+
+  constexpr auto y = &X<false>::f;
+  static_assert(__is_same(decltype(y), int(*const)(short)));
+  static_assert(y(0) == 24, "");
+  
+  constexpr auto z = &f<int>;
+  static_assert(__is_same(decltype(z), int(*const)(int)));
+  static_assert(z(0) == 2, "");
+
+  // C++ [temp.deduct.call]p6:
+  //   If the argument is an overload set containing one or more function templates,
+  //   the parameter is treated as a non-deduced context.
+  auto w = f; // since-cxx20-error {{variable 'w' with type 'auto' has incompatible initializer of type '<overloaded function type>'}}
+}
+
+} // namespace Example2
+#endif
+
+#if __cplusplus >= 201103L
+namespace Example3 {
+
+template <typename T> void f(T &&, void (*)(T &&)); // #cwg2918_f
+
+void g(int &); // #1
----------------
Endilll wrote:

`#1` is a valid marker for `VerifyDiagnosticConsumer`. It doesn't seem to be useful for you test, so I suggest to drop it together with `#2` and `#3`.

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


More information about the cfe-commits mailing list