0

In the code below a template class list declared as a friend of a template class link to make PrivateType available in class link:

#include <iostream>

template <class D>
class link
{
private:

    using PrivateType = int;

    template <class T1, class D1> friend class list;
};

template <class T, class D>
class list
{
private:

    using MyPrivateType = link<D>::PrivateType;

public:

    void add(MyPrivateType val)
    {
        std::cout << val << std::endl;
    }
};

int main()
{
    list<int, int> l;
    l.add(5);
}

Is the friend class declared in a right way?

I also tried:

template <class T> friend list<T, D>;

but it does not compile.

EDIT1:

Doesn't the code declare redundant friends? For example, list<std::string, std::vector> becomes a friend of link<int> while I need list<T, D> be a friend of link<D> for any given T and D classes.

Dmitriano
  • 1,878
  • 13
  • 29

0 Answers0