c++ – Friendly template class

Question:

Почему gcc выдаёт ошибку?

[Error] specialization of ‘template class A’ must appear at namespace scope

template<class T1, class T2>
class A
{
    template<class T3>
    friend class A<T1, T3>;
};

Answer:

There is no way in C++ to use a partial specialization to "skin" only a subset of a template's specializations as friends.

14.5.4 Friends

8 Friend declarations shall not declare partial specializations. [Example:

  template<class T> class A { };
  class X {
    template<class T> friend class A<T*>; // error
  };

–end example]

That is, a friend can be either a template (with all its specializations) or a specific specialization (i.e., a full specialization) of the template.

Scroll to Top