[PATCH] D16989: Change interpretation of function definition in friend declaration of template class.

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 8 08:20:02 PST 2016


sepavloff created this revision.
sepavloff added a reviewer: rsmith.
sepavloff added a subscriber: cfe-commits.

Previously if a file-level function was defined inside befriending
template class, it was treated as defined in the code like:
```
  void func(int);
  template<typename T> class C1 {
    friend void func(int) {}
  };
  void m() {
    func(1);
  }
```

The Standard requests ([temp.friend], p5) that `When a function is defined in
a friend function declaration in a class template, the function is instantiated
when the function is odr-used`. This statement however does not specify which
template is used for the instantiation, if the function is defined in several,
and what parameters are used for the instantiation. The latter is important if
the function implementation uses template parameter, such usage is not prohibited
by the Standard.

Other compilers (gcc, icc) follow viewpoint that body of the function defined
in friend declaration becomes available when corresponding class is instantiated.
This patch implements this viewpoint in clang.

Definitions introduced by friend declarations in template classes are added to
the redeclaration chain of corresponding function, but they do not make the
function defined. Only if such definition corresponds to the template
instantiation, the function becomes defined. Presence of function body is not
enough for function definition anymore.

This change fixes PR17923, PR22307 and PR25848.

http://reviews.llvm.org/D16989

Files:
  include/clang/AST/Decl.h
  lib/AST/Decl.cpp
  lib/CodeGen/CodeGenModule.cpp
  lib/Sema/Sema.cpp
  lib/Sema/SemaDecl.cpp
  lib/Sema/SemaTemplateInstantiateDecl.cpp
  test/SemaCXX/PR25848.cpp
  test/SemaCXX/friend2.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16989.47207.patch
Type: text/x-patch
Size: 9834 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160208/7cf61042/attachment-0001.bin>


More information about the cfe-commits mailing list