简体   繁体   English

与其他模板化类中的所有模板化类成为朋友

[英]Befriending all templated classes in other templated class

Look at following code: 看下面的代码:

template <typename T, int d>
class Grid {
   //Following line is what I need to change
   template<int d2> friend class Iterator<T,d,d2>;
}

template <typename T, int d, int d2>
class Iterator{ 
    //some code that use private fields of Grid<T,d>
}

template <typename T, int d>
class Iterator<T,d,0>{
    //This specialized class also need to use private parts of Grid<T,d>
}

Both specialized and not specialized Iterator should have access to private parts. 专用迭代器和非专用迭代器都应可以访问私有部分。 Line: 线:

template<int d2> friend class Iterator<T,d,d2>;

does not compile with error: partial specialization `Iterator' declared friend 不会编译错误:部分专业化`Iterator'声明为好友

Does anybody know how to replace it? 有人知道如何更换吗?

EDIT: Thanks to @Xeo comment i was able to make a workaround: 编辑:感谢@Xeo评论,我得以解决:

template<typename TT, int dd, int d2> friend class Iterator;

However this gives friend access to all Iterator templates not only to those one that have matching first and second template parameter. 但是,这不仅使朋友具有对所有Iterator模板的访问权,而且不仅仅对具有匹配的第一模板参数和第二模板参数的模板进行访问。

这只是一种解决方法,但它可以正常工作:

template<typename TT, int dd, int d2> friend class Iterator;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM