views:

65

answers:

1

Hello, 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:

+1  A: 

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.

Kirill V. Lyadvinsky
Thank you, it seems indeed to be a VC8 bug. Unfortunately, I cannot change the compiler, so I'll go with public members.
Alexandre C.
[This](https://connect.microsoft.com/VisualStudio/feedback/details/262353/compile-error-when-making-a-template-class-a-friend-of-itself-works-on-vc7-1-and-gcc-3-4-4#details) bug is very likely the same issue. Consider installing service pack for VC8.
Kirill V. Lyadvinsky