如何与 "template using" 定义的模板(别名)class 成为好友?
How to friend with a template (alias) class defined by "template using"?
Class B<certain X>
想和每个 C<any,certain X>
.
成为朋友
我正在拔头发,想知道怎么做。
只要我不添加有问题的行,下面是编译成功的完整代码。
#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;
}
这是我尝试过的方法(全部失败):-
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;
问题:要插入的正确语句(1 行)是什么?
如果可能的话,我希望 friend 语句使用 CT
而不是 C
。
我看过a similar question and this,但比我的简单
(我是 C++ 新手。)
Class B wants to be a friend with every C.
不幸的是,这是不可能的,因为 template friend 不能引用偏特化。而且这个问题与using
.
无关
Friend declarations cannot refer to partial specializations, but can refer to full specializations
如果枚举 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>;
...
};
Class B<certain X>
想和每个 C<any,certain X>
.
成为朋友
我正在拔头发,想知道怎么做。
只要我不添加有问题的行,下面是编译成功的完整代码。
#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;
}
这是我尝试过的方法(全部失败):-
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;
问题:要插入的正确语句(1 行)是什么?
如果可能的话,我希望 friend 语句使用 CT
而不是 C
。
我看过a similar question and this,但比我的简单
(我是 C++ 新手。)
Class B wants to be a friend with every C.
不幸的是,这是不可能的,因为 template friend 不能引用偏特化。而且这个问题与using
.
Friend declarations cannot refer to partial specializations, but can refer to full specializations
如果枚举 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>;
...
};