views:

666

answers:

2

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 ?

+9  A: 
class IWantToBeFriendsWithMyStruct
{
    template <typename T, typename U>
    friend struct MyStruct;
};

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

Rob Walker
Cool ! that works (I can't vote yet, I will when registered)
David
Note that this gives all types of MyStruct access to IWantToBeFriends, it is also possible to grant specific specializations of MyStruct access.
Greg Rogers
It works in g++ as well.
Martin York
+6  A: 

According to this site, the correct syntax would be

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