Search code examples
c++templatesfriendnested-class

Template friend and nested classes


please consider the following code:

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

I'd like all instantiations of foo<T>::bar to be friends of foo<T>::bar<S> for any S. If bar is not a nested template, the syntax above works just fine. But when I do for example

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

MSVC8 (Visual C++ 2005) doesn't like it:

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

The compiler gives me the same errors if I use

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

instead. How can I achieve what I want ?

EDIT: I double checked (it's morning here, and I'm not really awake), this seems to be a VC8 bug:


Solution

  • All restrictions for friend of a class or class template are described in section 14.5.3 of the C++ Standard. Your code is valid. Check you have installed all latest service packs for the Visual Studio. Here you can find related bugs in Visual Studio.