Friend classes or functions can be declared within a class template.
When a template is instantiated, the names of its friends are treated
as if the specialization had been explicitly declared at its point of
instantiation.
As with non-template classes, the names of namespace-scope friend
functions of a class template specialization are not visible during
an ordinary lookup unless explicitly declared at namespace scope ([class.friend]).
[Example 1: template<typename T>struct number {
number(int);
friend number gcd(number x, number y){return0; };
};
void g(){
number<double> a(3), b(4);
a = gcd(a,b); // finds gcd because number<double> is an associated class,// making gcd visible in its namespace (global scope)
b = gcd(3,4); // error: gcd is not visible} — end example]