如何将结构模板标记为朋友?

时间:2008-10-15 19:19:34

标签: c++ class templates syntax friend

我有这样的代码:

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

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

为模板提供友谊的正确语法是什么?

2 个答案:

答案 0 :(得分:18)

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

在VS2008中工作,并允许MyStruct访问该类。

答案 1 :(得分:7)

根据this site,正确的语法是

class IWantToBeFriendsWithMyStruct
{
    template <typename T, typename U> friend struct MyStruct; 
}
相关问题