Why does gcc give an error?

[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>; }; 

    2 answers 2

    In C ++, it is not possible to use partial specialization in order to “outline” only a subset of the template specializations as friends.

    14.5.4 Friends

    8 Friend declarations partial declarations. [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 (that is, full specialization) of a template.

    • Well, I felt that there would be no specialization :) Thanks for pointing out the standard. - Harry
    • it is sad; but I can specify all the parameters, and those that should remain unchanged just compare with std::is_same ? - user230240

    Remove <T2> :

     template<class T> class A { template<class T2> friend class A; }; 

    Update:

    well ... again -

     template<class T1, class T3> friend class A; 

    However, it is not very sure that it will work specialization.

    • and if I have several parameters and I want to specifically add this specialization to friends? - user230240
    • changed the question. - user230240
    • Added in response. - Harry
    • So you have 2 arguments, but I need some amount to be preserved ( T1 ), while others have changed ( T2 -> T3 ) - user230240
    • Those. need to use specialization - user230240