[PATCH] D16579: Warn if friend function depends on template parameters.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 18 06:25:01 PDT 2016


rsmith added a subscriber: rsmith.
rsmith added a comment.

It seems useful to warn on this, but I'm concerned that there are valid code patterns that this will warn about where there is no reasonable way to rewrite the code that would suppress the warning. For instance:

  template<typename T> class X {
    friend void process(X<T>);
    // ...
  };
  void process(X<int>) { /* ... */ }
  void process(X<char>) ( /* ... */ }

As an alternative, can we suppress the warning until we see an "almost matching" template declaration in the surrounding scope? For instance:

  template<typename T> void process(X<T>) { /* ... */ } // warning, not a friend of X<T>


================
Comment at: include/clang/Basic/DiagnosticSemaKinds.td:1155-1157
@@ -1153,1 +1154,5 @@
+  "non-template function">, InGroup<NonTemplateFriend>;
+def note_non_template_friend : Note<"if this is not what you intended, make "
+  "sure the function template has already been declared and add <> after the "
+  "function name here">;
 
----------------
Maybe just: "to befriend a template specialization, use <>", along with a FixItHint showing where to add the <>.

It would also seem like a good idea to check if there is such a function template declared, and give different diagnostic if not.


http://reviews.llvm.org/D16579





More information about the cfe-commits mailing list