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

Arthur O'Dwyer via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 17 11:35:19 PDT 2016


I'm not qualified to comment on the implementation, but I'm a bit skeptical
that this warning is appropriate in the first place. I've often declared
friend non-template functions, e.g. swap(). I've never intended to declare
a friend *template specialization*. Is declaring friend template
specializations something that people do often? Is it something that Clang
should be encouraging newbies to do *more* often?

GCC suppresses the diagnostic when the friend function is declared inline,
which is good, because it shuts up the diagnostic in this very common case:

template<class T>
struct vector {
    void swap(vector& b) { /* swap members */ }
    friend void swap(vector& a, vector& b) { a.swap(b); }  // friend
non-template function
};

IMHO you should add an explicit test confirming that the warning is
suppressed in this case.

The example in PR23342 doesn't seem like a good example, because GCC gives
not only the proposed warning for that code, but also a linker error; i.e.,
there's no danger of someone accidentally getting wrong runtime behavior
there. Is there a compelling example of how someone could write code that
has wrong runtime behavior, *and* which code would be *fixed* by the
suggested addition of template<>? I think it's much more likely that the
appropriate fix would be to move the function definition inline.
https://llvm.org/bugs/show_bug.cgi?id=23342

Why did you need to suppress the warning in
"test/CXX/temp/temp.decls/temp.friend/p1.cpp"? Unless I'm mistaken, GCC
doesn't give the diagnostic on that file (and nor should it).

my $.02,
–Arthur



On Thu, Mar 17, 2016 at 9:21 AM, Serge Pavlov via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> Ping.
>
> Thanks,
> --Serge
>
> 2016-02-26 12:20 GMT+06:00 Serge Pavlov <sepavloff at gmail.com>:
>
>> Could someone provide a feedback?
>>
>> Thanks,
>> --Serge
>>
>> 2016-01-26 20:55 GMT+06:00 Serge Pavlov <sepavloff at gmail.com>:
>>
>>> sepavloff created this revision.
>>> sepavloff added a subscriber: cfe-commits.
>>>
>>> Declaration of friend function may depend on template parameters,
>>> however it does not become a template function:
>>>
>>>     template<typename T> class C1 {
>>>       friend void func(T x);
>>>     };
>>>
>>> It may be not obvious for user, so compiler could emit a warning in
>>> such case. This patch implements appropriate warning, the wording is
>>> taken from GCC message. The patch fixes PR23342.
>>>
>>> http://reviews.llvm.org/D16579
>>>
>>> Files:
>>>   include/clang/Basic/DiagnosticGroups.td
>>>   include/clang/Basic/DiagnosticSemaKinds.td
>>>   lib/Sema/SemaDecl.cpp
>>>   test/CXX/drs/dr3xx.cpp
>>>   test/CXX/drs/dr5xx.cpp
>>>   test/CXX/temp/temp.decls/temp.friend/p1.cpp
>>>   test/PCH/cxx-templates.cpp
>>>   test/PCH/cxx-templates.h
>>>   test/SemaCXX/friend.cpp
>>>   test/SemaCXX/overload-call.cpp
>>>
>>>
>>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160317/8e99d281/attachment.html>


More information about the cfe-commits mailing list