Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template friend class with more template parameters

Tags:

c++

I have a class template Obj1 with three template parameters

template < class A, class B, class C >
class Obj1
{
      // some implementation
};

and the second class template Obj2 with two template parameters,

 template < class A, class B >
    class Obj2
    {
          // some implementation
    };

so my problem is the following:

I want to make the class Obj1 to be the friend of class Obj2, with the first two template parameters with the same value, but I do not know the exact syntax how to write it, At first i tryed this way

template < class A, class B>
class Obj2
{
    template< class C>
    friend class Obj1<A,B,C>;
};

but it did not compile, so pleas help me if you can.

like image 972
zapredelom Avatar asked Oct 21 '22 07:10

zapredelom


1 Answers

The answer to your question is here: https://stackoverflow.com/a/1458897/2436175 "You can only 'befriend' all instantiations of a class template or specific full specializations."

So, this is what is allowed:

template < class A, class B, class C >
class Obj1
{
      // some implementation
};

template < class A, class B>
class Obj2
{
    public:
    template <class C, class D, class E> friend class Obj1; ///All instances
};

template < class A, class B>
class Obj3
{
    public:
    friend class Obj1<A,B,int>; ///One specific instantiation, possibly depending from your own templated parameters
    friend class Obj1<A,B,A>; ///One specific instantiation, possibly depending from your own templated parameters
    friend class Obj1<char,float,int>; ///One specific instantiation, possibly depending from your own templated parameters
};
like image 174
Antonio Avatar answered Oct 23 '22 03:10

Antonio