【问题标题】:Befriending all templated classes in other templated class与其他模板类中的所有模板类成为朋友
【发布时间】:2012-03-14 10:33:28
【问题描述】:

看下面的代码:

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;

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

【问题讨论】:

  • 如果在定义Grid 之前先声明Iterator 会怎样?我认为friend 特定版本的Iterator 声明将起作用。使类或非内联函数的friend 声明成为该项目的第一个声明似乎永远不会完全正确。 (我不知道您是否还必须转发声明Iterator&lt;T,d,0&gt;。)

标签: c++ templates friend


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多