C++ 如何将模板化结构/类声明为朋友?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3292795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare a templated struct/class as a friend?
提问by Alexandre C.
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:
但我的编译器 (VC8) 窒息了:
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
.
我想拥有for all的template struct foo
朋友的所有可能的实例。foo<T>
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.
似乎可以编译,但它正确吗?朋友和模板的语法非常不自然。
回答by Anycorn
template<typename> friend class foo
this will however make all templates friends to each other. But I think this is what you want?
然而,这将使所有模板彼此成为朋友。但我想这就是你想要的吗?