70

I'd like to do the following:

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

private:
    // ...
};

but my compiler (VC8) chokes on it:

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

I'd like to have all possible instantiations of template struct foo friends of foo<T> for all T.

How do I make this work ?

EDIT: This

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

private:
    // ...
};

seems to compile, but is it correct ? Friends and templates have very unnatural syntax.

1 Answer 1

111
template<typename> friend class foo

this will however make all templates friends to each other. But I think this is what you want?

2
  • 5
    can you please post reference section to standard where it's defined?
    – Mr.Anubis
    Mar 5, 2012 at 18:20
  • 12
    In C++03 in 14.5.3.3 and 14.5.3.4, In C++11 you need to read the Friends section in 14.5.4
    – Muhammad
    Apr 4, 2014 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.