[英]How to make a friend of a partially specialized template class to another partially specialized template class?
I require a template specialised class to make friend
with another template specialised class. 我需要一个模板专用类来与另一个模板专用类交
friend
。
Example: 例:
template <class A, class B>
class X { };
template <class A, class B>
class Y { };
Now I want to have only some specific specialisations of these template classes to be friends: 现在,我只希望这些模板类的某些特定专业成为朋友:
I tried 我试过了
template <class A>
class X <A, bool> { };
template <class A>
class Y <A, bool>
{
// X<A, bool> shall be friend!
template<class _C> using X_partial = X<_C,bool>;
template<class _D> friend class X_partial;
};
and this, 和这个,
template <class A>
class X <A, bool> { };
template <class A>
class Y <A, bool>
{
// X<A, bool> shall be friend!
template<class C> friend class X<class C,bool>
};
But both gives the error . 但是两者都给出了错误。
The syntax can support C++(03), C++11 or C++14. 该语法可以支持C ++(03),C ++ 11或C ++ 14。
The friend declaration cannot declare a partial specialization. 朋友声明不能声明部分专业化。 See here .
看这里 。
Friend declarations shall not declare partial specializations.
朋友声明不得声明部分专业。 [ Example:
[示例:
template<class T> class A { }; class X { template<class T> friend class A<T*>; // error };
— end example ]
—结束示例]
However, if the "initial" types for A
are the same, then to provide friendship to the correct specialization, it is as follows; 然而,如果“初始”类型
A
是相同的,然后友情提供到正确的专业化,它是如下;
template <class A, class B>
class Y { };
template <class A, class B>
class X {
public:
// fails to compile when instantiated
//X() { Y<A, bool> y; y.i = 42; }
};
template <class A>
class Y <A, bool>
{
// partial specialisation provided friendship
friend class X<A, bool>;
int i = 0;
};
template <class A>
class X <A, bool> {
public:
X() { Y<A, bool> y; y.i = 42; }
};
int main()
{
X<int, bool> x1;
// fails to compile.
X<int, int> x2;
}
Sample online . 在线样品 。
Where is a problem in that? 哪里有问题? What have you tried before asking this question?
在问这个问题之前,您尝试过什么?
// Templated class X
template <class A, bool>
class X {
};
// Templated class Y
template <class A, bool>
class Y {
// Any specialized A is a friend of any specialized B
template <class B, bool> friend class X;
};
Edit: Update with classes you provided in comment on your question. 编辑:更新您在问题评论中提供的课程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.