wants to be a friend with everyone C

How do I contact a template class (alias) defined by "template usage"?

The class B<certain X>wants to be a friend with everyone C<any,certain X>.
I stretch my hair to find how to do it.

Below is the complete code that compiles successfully until I add the problem line.

#include <string>
using namespace std;

enum EN{ EN1,EN2 };
template<EN T1,class T2> class C{
    public: C(){
        std::cout<<T1<<std::endl;   
    }
};
template<class T2> class B{
    template<EN T1> using CT = C<T1,T2>;
    //template<EN TX> friend class CT;  //<-- error if insert this line
    public: static void test(){
        CT<EN1> ct;
    }
};

int main() {
    B<int>::test();
    return 0;
}

Here is what I tried (all failed): -

template<EN T1> friend class C<T1,T2>;  
template<EN TX> friend class CT;  
template<typename TX> friend class CT; 
template<class TX> friend class CT; 
template<class TX> friend class CT<TX>;    
template<typename> friend typename CT; 

Question: What is the correct statement (1 line) to insert?
If possible, I want the friend statement to use CT, not C.

I read a similar question and this one , but they are easier than mine.
(I am new to C ++.)

+4
source share
1

B C.

, , . using.

,

, EN . .

template<class T2> class B {
    template<EN T1> using CT = C<T1, T2>;
    friend CT<EN1>; // same as friend C<EN1, T2>;
    friend CT<EN2>; // same as friend C<EN2, T2>;
    ...
};
+4

Source: https://habr.com/ru/post/1669779/


All Articles