How to have a template class as friend?

C

Chris Dams

Dear all,

I would like to have a template class as friend of another class. If I write

class c
{ friend class t;
};

template <class T> class t
{
};

then this is not allowed and on my g++ version 4.0.2. It says that
"t is not a template type". Apparently this is because of the friend
declaration, because if I comment it out, the code compiles without errors.
But I want t to be a friend of c no matter what the template argument
of t is. Can anyone tell me how to do this?

Many thanks,
Chris
 
V

Victor Bazarov

Chris said:
I would like to have a template class as friend of another class. If
I write

class c
{ friend class t;
};

template <class T> class t
{
};

then this is not allowed and on my g++ version 4.0.2. It says that
"t is not a template type". Apparently this is because of the friend
declaration, because if I comment it out, the code compiles without
errors. But I want t to be a friend of c no matter what the template
argument
of t is. Can anyone tell me how to do this?

class c
{
void foo(); // private
template<class T> friend class t; // 't' is a template
};

template<class T> class t {
public:
void bar(c& cc) { cc.foo(); }
};

int main() {
t<int> tt;
c cc;
tt.bar(cc);
}


V
 
H

Howard

class c
{
void foo(); // private
template<class T> friend class t; // 't' is a template
};

template<class T> class t {
public:
void bar(c& cc) { cc.foo(); }
};

int main() {
t<int> tt;
c cc;
tt.bar(cc);
}

I was just wondering that myself. Thanks, Victor.

(P.S., "tt.bar"? Haven't been to one of those in a while...)
 
V

Victor Bazarov

Howard said:
[..]
(P.S., "tt.bar"? Haven't been to one of those in a while...)

I've never been to any of those! I just heard about them on t.vee() :)

V
 
C

Chris Dams

Dear Victor,

Victor Bazarov said:
template<class T> friend class t; // 't' is a template

Thanks!

I had instead been trying variations of

friend template<class T> class t;

and that didn't work.

Best wishes,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top