Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `template <class> friend class Foo` mean?

Tags:

c++

boost

friend

I'm exploring the boost::iterator_facade and came across this bit of code:

    friend class boost::iterator_core_access;
    template <class> friend class Iterator;

What does the second line mean? I'm familiar with friend classes, but I don't think I've seen template <class> in front of anything before.


Here's the context:

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;
like image 482
vmrob Avatar asked Jun 06 '14 01:06

vmrob


People also ask

What does class template mean?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template. is a template used to generate template classes.

What do you mean by friend class?

Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.

What is the use of template class?

Class Templates like function templates, class templates are useful when a class defines something that is independent of the data type. Can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc.

How do I declare a friend template class in C++?

The following example demonstrates these relationships: class B{ template<class V> friend int j(); } template<class S> g(); template<class T> class A { friend int e(); friend int f(T); friend int g<T>(); template<class U> friend int h(); };


1 Answers

It just means Iterator is a template class with one template parameter. The friendship is granted to all instantiations of Iterator.

Iterator<int> is a friend of the class.

Iterator<bool> is a friend of the class.

...

Iterator<MyClass> is a friend of the class.

You get the idea.

Example Usage

Say you have a class template Foo.

template <typename T> class Foo
{
   public:
      Foo() : data(0) {}
   prvavte:
      T data;
};

When you instantiate the class template using:

Foo<int> a;
Foo<float> b;

you are creating two classes at compile time. Foo<int> does not have access to the private section of Foo<float> and vice versa. That is an inconvenience some times.

You can't do:

b = a;  // If you wanted to pull the data from a and put it in b.

Even if you added an assignment operator to the class,

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

It won't work because Foo<T> doesn't have access to the private sections of Foo<T2>. To get around that you can use a friend declaration.

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

Now, you can use:

Foo<int> a;
Foo<float> b;
b = a;
like image 69
R Sahu Avatar answered Sep 27 '22 23:09

R Sahu