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

发布于 2025-01-08 05:20:51 字数 818 浏览 4 评论 0原文

看下面的代码:

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

编译时出现错误:部分专业化“迭代器”声明的朋友

有人知道如何替换它吗?

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

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

但是,这使朋友可以访问所有迭代器模板,而不仅仅是那些具有匹配的第一个和第二个模板参数的模板。

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

Does anybody know how to replace it?

EDIT:
Thanks to @Xeo comment i was able to make a workaround:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2025-01-15 05:20:51

这只是一个解决方法,但它正在起作用:

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

This is just a workaround but it is working:

template<typename TT, int dd, int d2> friend class Iterator;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文