0

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?

Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • 3
    Define _related_ in more detail please. `friend` declarations are not the same or somehow related to `extern` declarations. Do you have a certain standard section in mind? – πάντα ῥεῖ Oct 01 '15 at 20:47

1 Answers1

1

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).

davmac
  • 20,150
  • 1
  • 40
  • 68
  • Wouldn't a friend declaration with such explicit template arguments be considered an implicit instantiation? That would be almost related to an explicit instantiation (without the `extern`). – David G Oct 01 '15 at 20:59
  • @0x499602D2 I see what you are saying, but I don't think it does qualify as an implicit instantiation. g++ seems to agree with me - a quick test shows that no instantiation is emitted. – davmac Oct 01 '15 at 21:04
  • 1
    From the spec: a "function template specialization is implicitly instantiated when the specialization is referenced in a context that requires a function definition to exist" – davmac Oct 01 '15 at 21:19
  • @davmac I'm wondering why the term *specialization* is used in the quoted spec. I think it should have nothing to do with template specialization anyway. – Lingxi Oct 02 '15 at 02:48
  • 1
    @Lingxi I gather that the spec refers to any template with type arguments applied as a specialization. A specialization providing an alternative definition is an _explicit_ specialization: "An explicit specialization may be declared for a function template, a class template, a member of a class template or a member template. An explicit specialization declaration is introduced by template<>" – davmac Oct 02 '15 at 07:26