Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to declare a template class as a friend in c++

Tags:

c++

I wana know if we can make a partial specialized class as a friend class.

template< class A, class B >
class AB{};

class C;

template < class B >
class AB< C, B >{};

class D{
     template< class E > friend class AB< D, E >;
}

How to achieve the above.

like image 758
rkb Avatar asked Sep 17 '10 14:09

rkb


1 Answers

This is not allowed by the C++ Standard (§14.5.3/9):

Friend declarations shall not declare partial specializations. [Example:

template<class T> class A { };
class X {
    template <class T> friend class A<T*>;   //error
};

--end example]

So basically, you can either make all instantiations of AB friend of D or only one particular instantiation. This IBM page describes the different relationships that can be achieved when it comes to friends and templates: "one-to-one", "one-to-many", "many-to-one" and "many-to-many" (but not "one-to-some" as you asked).

like image 178
Luc Touraille Avatar answered Oct 13 '22 00:10

Luc Touraille