【问题标题】:How do you mark a struct template as friend?你如何将结构模板标记为朋友?
【发布时间】:2010-09-17 09:20:37
【问题描述】:

我有这样的代码:

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

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

赋予模板友谊的正确语法是什么?

【问题讨论】:

    标签: c++ class templates syntax friend


    【解决方案1】:
    class IWantToBeFriendsWithMyStruct
    {
        template <typename T, typename U>
        friend struct MyStruct;
    };
    

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

    【讨论】:

    • 请注意,这使所有类型的 MyStruct 都可以访问 IWantToBeFriends,也可以授予特定的 MyStruct 访问权限。
    【解决方案2】:

    根据this site,正确的语法应该是

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

    【讨论】:

      猜你喜欢
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2017-08-08
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多