【问题标题】:Template friend and nested classes模板朋友和嵌套类
【发布时间】:2011-03-18 21:09:41
【问题描述】:

请考虑以下代码:

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

我希望foo&lt;T&gt;::bar 的所有实例都成为foo&lt;T&gt;::bar&lt;S&gt; 的任何S 的朋友。如果bar 不是嵌套模板,那么上面的语法就可以正常工作。但是当我这样做时

int main() 
{  
    foo<int> x;
}

MSVC8 (Visual C++ 2005) 不喜欢它:

1>.\main.cpp(11) : error C3855: 'foo<T>::bar': template parameter 'S' is incompatible with the declaration
1>        .\main.cpp(12) : see reference to class template instantiation 'foo<T>::bar<S>' being compiled
1>        .\main.cpp(14) : see reference to class template instantiation 'foo<T>' being compiled

如果我使用编译器会给我同样的错误

template <typename> friend struct foo<T>::bar;

相反。我怎样才能达到我想要的?

编辑:我仔细检查了(这里是早上,我还没醒),这似乎是VC8 bug

【问题讨论】:

  • 似乎在我的机器上工作。相同的编译器。
  • 我复制粘贴了上面的确切代码,没有包含,我复制粘贴了错误消息。

标签: c++ templates friend nested-class


【解决方案1】:

C++ 标准的14.5.3 部分描述了对类或类模板的友元的所有限制。您的代码有效。检查您是否已为 Visual Studio 安装了所有最新的服务包。 Here你可以在Visual Studio中找到相关的bug。

【讨论】:

  • 谢谢,看来确实是VC8的bug。不幸的是,我无法更改编译器,所以我将使用公共成员。
  • This 错误很可能是同一个问题。考虑为 VC8 安装服务包。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2011-10-14
  • 1970-01-01
相关资源
最近更新 更多