【问题标题】:How to declare a templated struct/class as a friend?如何将模板化结构/类声明为朋友?
【发布时间】:2011-03-18 14:14:22
【问题描述】:

我想做以下事情:

template <typename T>
struct foo
{
    template <typename S>
    friend struct foo<S>;

private:
    // ...
};

但是我的编译器 (VC8) 卡住了:

error C3857: 'foo<T>': multiple template parameter lists are not allowed

我希望所有Tfoo&lt;T&gt; 朋友的template struct foo 的所有可能实例化。

我该如何进行这项工作?

编辑:这个

template <typename T>
struct foo
{
    template <typename>
    friend struct foo;

private:
    // ...
};

似乎可以编译,但它正确吗?好友和模板的语法非常不自然。

【问题讨论】:

    标签: c++ templates friend


    【解决方案1】:
    template<typename> friend class foo
    

    然而,这将使所有模板彼此成为朋友。 但我想这就是你想要的?

    【讨论】:

    • 您能否将参考部分发布到定义它的标准?
    • 在 C++03 的 14.5.3.3 和 14.5.3.4 中,在 C++11 中你需要阅读 14.5.4 中的 Friends 部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多