在其他模板类中与所有模板化的类交朋友

时间:2012-02-23 23:08:04

标签: c++ templates friend

请看以下代码:

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

专用和非专用迭代器都应该可以访问私有部分。 行:

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

没有编译错误:部分特化`Iterator'声明了朋友

有人知道如何更换吗?

编辑: 感谢@Xeo评论我能够解决方法:

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

然而,这使得朋友不仅可以访问具有匹配的第一和第二模板参数的所有Iterator模板。

1 个答案:

答案 0 :(得分:1)

这只是一种解决方法,但它正在运作:

template<typename TT, int dd, int d2> friend class Iterator;
相关问题