Group: comp.lang.c++
From: Jeff Schwab
Date: Sunday, February 17, 2008 3:46 PM
Subject: Re: template friend class

Kira Yamato wrote:
> A question about declaring a friend class. The following does not compile:
>
> template
> class B
> {
> public:
>
> friend class C;
> };
>
> g++4.0.1 returns the error message
>
> error: using template type parameter 'C' after 'class'
>
>
> So, is there a way to declare a friend class from the template parameter?

NB: I'm not sure why you want to do this, and I don't necessarily
endorse it.

AFAIK, you would have to specialize the template explicitly for each
parameter type. For example:

template struct B;

struct S { };

template<> struct B { friend class S; };

You could write a macro to create specializations:

template struct B;

#define SPECIALIZE_B(param) \
template<> struct B { friend class param; }

struct S { };
struct T { };

SPECIALIZE_B(S);
SPECIALIZE_B(T);

Btw, why do you actually want this feature?