template friend class

Apr 13, 2009 at 4:28am
I have a template class who needs to be friends of all other classes made from that template.

ie: MyClass<int> needs to be friends with MyClass<float>... however because this is a template that will be used with many user-defined types I can't just list all the possible friends in the class.

I tried following stuff, none of which worked:

1
2
3
4
5
6
7
8
9
template <typename T>
class MyClass
{
friend class MyClass;    // 'MyClass' already friends with itself
friend class MyClass <>; // wrong number of template arguments

template <typename TT> friend class MyClass<TT>;
    //partial specialization 'MyClass<TT>' declared 'friend'
};


Is this possible?

Thanks in advance


-- [ EDIT ]--

of course it's the one thing I didn't try:

1
2
3
4
5
template <typename T>
class MyClass
{
  template <typename TT> friend class MyClass;  // not MyClass<TT>
};


Seems so obvious now. Thanks
Last edited on Apr 13, 2009 at 11:06am
Apr 13, 2009 at 12:21pm
It should already be friends without having to make any additional declarations.

yup, so you're right now that i tried it.
Last edited on Apr 13, 2009 at 12:40pm
Apr 13, 2009 at 12:31pm
nah -- because MyClass<int> and MyClass<float> are two seperate classes -- they're not friends with each other by default.
Topic archived. No new replies allowed.