zapredelom
zapredelom

Reputation: 1029

template friend class with more template parameters

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.

Upvotes: 5

Views: 426

Answers (1)

Antonio
Antonio

Reputation: 20296

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

Upvotes: 4

Related Questions