Search code examples
c++templatesc++11language-lawyerexplicit-instantiation

Friend declaration and explicit template instantiation declaration


Suppose I have a function template

template <typename T>
void f(T) {}

Then, we could have a friend declaration

friend void f<int>(int);

and an explicit template instantiation declaration

extern template void f<int>(int);

Are the two declarations related in some way or totally independent? If related, how do they interact with each other?


Solution

  • A friend declaration doesn't "interact" with any thing, other than the definition of the function which is declared to be a friend, which is allowed to access private members of the class containing the declaration.

    So, no, there is no special interaction between the friend declaration and the extern template declaration, although they both refer to the same function (in this sense, they are related, but I assume you realised this when you wrote the question).