`template <class>朋友类Foo`是什么意思?

vmr*_*rob 5 c++ boost friend

我正在探索boost :: iterator_facade并遇到了这段代码:

    friend class boost::iterator_core_access;
    template <class> friend class Iterator;
Run Code Online (Sandbox Code Playgroud)

第二行是什么意思?我熟悉朋友课程,但我认为我以前没见过template <class>任何东西.


这是上下文:

template <class Value>
class node_iter
  : public boost::iterator_facade<
        node_iter<Value>
      , Value
      , boost::forward_traversal_tag
    >
{
 public:
    node_iter()
      : m_node(0) {}

    explicit node_iter(Value* p)
      : m_node(p) {}

    template <class OtherValue>
    node_iter(node_iter<OtherValue> const& other)
      : m_node(other.m_node) {}

 private:
    friend class boost::iterator_core_access;
    template <class> friend class node_iter;

    template <class OtherValue>
    bool equal(node_iter<OtherValue> const& other) const
    {
        return this->m_node == other.m_node;
    }

    void increment()
    { m_node = m_node->next(); }

    Value& dereference() const
    { return *m_node; }

    Value* m_node;
};
typedef impl::node_iterator<node_base> node_iterator;
typedef impl::node_iterator<node_base const> node_const_iterator;
Run Code Online (Sandbox Code Playgroud)

R S*_*ahu 5

它只是Iterator一个带有一个模板参数的模板类.友谊被授予所有的实例Iterator.

Iterator<int> 是班上的朋友.

Iterator<bool> 是班上的朋友.

...

Iterator<MyClass> 是班上的朋友.

你明白了.

示例用法

假设你有一个课程模板Foo.

template <typename T> class Foo
{
   public:
      Foo() : data(0) {}
   prvavte:
      T data;
};
Run Code Online (Sandbox Code Playgroud)

使用以下方法实例化类模板时:

Foo<int> a;
Foo<float> b;
Run Code Online (Sandbox Code Playgroud)

你是在编译时创建两个类.Foo<int>无法访问私有部分,Foo<float>反之亦然.这有点不方便.

你做不到:

b = a;  // If you wanted to pull the data from a and put it in b.
Run Code Online (Sandbox Code Playgroud)

即使您在课程中添加了赋值运算符,

template <typename T> class Foo
{
   public:
      Foo() : data(0) {}
      template <typename T2> Foo& operator=(Foo<T2> const& rhs)
      {
         this->data = rhs.data;
         return *this;
      }

   private:
      T data;
};
Run Code Online (Sandbox Code Playgroud)

它不起作用,因为Foo<T>无法访问私有部分Foo<T2>.为了解决这个问题,你可以使用朋友声明.

template <typename T> class Foo
{
   public:
      template <class> friend class Foo;
      Foo() : data(0) {}
      template <typename T2> Foo& operator=(Foo<T2> const& rhs)
      {
         this->data = rhs.data;
         return *this;
      }

   private:
      T data;
};
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用:

Foo<int> a;
Foo<float> b;
b = a;
Run Code Online (Sandbox Code Playgroud)