Jump to content
  • Advertisement
Sign in to follow this  
Zbyl

Templated "friend"

This topic is 4944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a small question. I want to do something like this:
template<class T>
class CClass {
    template<class U> friend CClass<U>; //it doesn't work...
    //error C2988: unrecognizable template declaration/definition
};

In other words I want to make any instance of the CClass template to be friend of any other. How to solve this problem?

Share this post


Link to post
Share on other sites
Advertisement
Try this maybe:


template<class T>
class Bar;

template<class T>
class Foo
{
template<class U>
friend Bar<U>;
};



I haven't verified this, just so you know.

Share this post


Link to post
Share on other sites
You can't have a template class be a friend (though you can make specific instantiations friends). Instead of trying to make all instantiations CClass be friends, abstract the parts which need to be accessed to a common base that all instantiations share, then befriend that.

Another option is to move the access to a template function whose definition is inlined into the class:

class a
{
template< typename A >
friend void func() {} // Makes all instantiations of func be a friend
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!