模板朋友和嵌套类

时间:2010-07-21 09:03:19

标签: c++ templates friend nested-class

请考虑以下代码:

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

我希望foo<T>::bar的所有实例化都成为任何foo<T>::bar<S> 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

1 个答案:

答案 0 :(得分:2)

类或朋友的所有限制在C ++标准的14.5.3部分中描述。您的代码有效。检查是否已安装Visual Studio的所有最新Service Pack。 Here您可以在Visual Studio中找到相关的错误。

相关问题