2008-10-15 3 views
10

다음과 같은 코드가 있습니다.어떻게 구조체 템플릿을 친구로 표시합니까?

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

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

템플릿에 우정을 제공하는 올바른 구문은 무엇입니까?

답변

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

VS2008에서 작동하며 MyStruct에서 클래스에 액세스 할 수 있습니다.

+0

쿨이 될 것입니다! 그 작품 (나는 아직 투표 할 수 없다, 내가 등록 할 때) – David

+0

이것은 모든 유형의 MyStruct가 IWantToBeFriends에 액세스 할 수있게 해 주며 MyStruct 액세스의 특정 전문을 부여 할 수도 있습니다. –

+0

g ++에서도 잘 작동합니다. –

7

this site에 따르면, 올바른 구문은

class IWantToBeFriendsWithMyStruct 
{ 
    template <typename T, typename U> friend struct MyStruct; 
} 
관련 문제