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.
Most of the logic in this function can be replaced by something much simpler, such as
Can you try to make such a simplification? Can we use the above as the criterion here?