朋友课程的部分模板专业化?
我有一个简单的X类,以及一组模板化的类y< t,u>。我希望所有的类y都恰好是x的所有类y。希望以下是我想要的,但是Friend语句给出了编译错误。
template<typename T, typename U>
class Y {
};
class X {
public:
X(int value) : i(value) {}
const int& getI() const { return i; }
private:
int i;
template<class U> friend class Y<X,U>;
};
我不确定完全允许使用朋友语句的模板化(更不用说朋友语句的部分模板化了)。有办法这样做吗?还是我一对一地列出所有朋友?
谢谢, 马特
I have a simple class X, and set of templatized classes Y<T,U>. I'd like all classes Y where the first templatization parameter happens to be X to be a friend of X itself. The following hopefully conveys what I want, but the friend statement gives a compile error.
template<typename T, typename U>
class Y {
};
class X {
public:
X(int value) : i(value) {}
const int& getI() const { return i; }
private:
int i;
template<class U> friend class Y<X,U>;
};
I'm not sure templatization of friend statements is allowed at all (let alone partial templatization of friend statements). Is there a way to do this? Or am I stuck listing out all the friends one-by-one?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
朋友声明页面在cppreference.com上指定:
因此Chtz表示您可以有一个非机构专业朋友。
编辑:
另请参阅stackoverflow上的另一个答案: https://stackoverflow.com/a/1046918/5776353
The friend declaration page on cppreference.com specifies:
So as chtz said you can have a non-partial specialization friend.
Edit:
See also another answer on stackoverflow: https://stackoverflow.com/a/11046918/5776353
对于您问题的非机构部分,语法是:
我想,在大多数情况下,这应该足够。
使用C ++ 11,您实际上可以成为模板的别名:
但是,这似乎不起作用(我不确定上面的朋友声明是否完全有任何影响)。
For the non-partial part of your question, the syntax is:
I guess, in most cases that should be sufficient.
With C++11 you can actually friend a templated alias:
However, that does not seem to work (I'm not sure if the friend declaration above has any effect at all).