11

I have code like this:

template <typename T, typename U> struct MyStruct {
    T aType;
    U anotherType;
};

class IWantToBeFriendsWithMyStruct
{
    friend struct MyStruct; //what is the correct syntax here ?
};

What is the correct syntax to give friendship to the template ?

Martin York
  • 257,169
  • 86
  • 333
  • 562
David
  • 9,635
  • 5
  • 62
  • 68

2 Answers2

18
class IWantToBeFriendsWithMyStruct
{
    template <typename T, typename U>
    friend struct MyStruct;
};

Works in VS2008, and allows MyStruct to access the class.

Martin York
  • 257,169
  • 86
  • 333
  • 562
Rob Walker
  • 46,588
  • 15
  • 99
  • 136
  • Note that this gives all types of MyStruct access to IWantToBeFriends, it is also possible to grant specific specializations of MyStruct access. – Greg Rogers Oct 15 '08 at 19:29
7

According to this site, the correct syntax would be

class IWantToBeFriendsWithMyStruct
{
    template <typename T, typename U> friend struct MyStruct; 
}
Lev
  • 6,487
  • 6
  • 28
  • 29