How to declare a templated struct/class as a friend?

落花浮王杯 提交于 2019-11-26 04:25:21

问题


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:


template<typename> friend class foo

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



来源:https://stackoverflow.com/questions/3292795/how-to-declare-a-templated-struct-class-as-a-friend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!