简体   繁体   English

如何在c ++中将模板类声明为朋友

[英]how to declare a template class as a friend in c++

I wana know if we can make a partial specialized class as a friend class. 我知道我们是否可以将部分专业课作为朋友班。

template< class A, class B >
class AB{};

class C;

template < class B >
class AB< C, B >{};

class D{
     template< class E > friend class AB< D, E >;
}

How to achieve the above. 如何实现上述目标。

This is not allowed by the C++ Standard (§14.5.3/9): C ++标准(第14.5.3 / 9节)不允许这样做:

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] - 末端示例]

So basically, you can either make all instantiations of AB friend of D or only one particular instantiation. 所以基本上,你既可以对DAB朋友进行所有实例化,也可以只对一个特定的实例进行实例化。 This IBM page describes the different relationships that can be achieved when it comes to friends and templates: "one-to-one", "one-to-many", "many-to-one" and "many-to-many" (but not "one-to-some" as you asked). 这个IBM页面描述了朋友和模板可以实现的不同关系:“一对一”,“一对多”,“多对一”和“多对多” (但不是你所问的“一对一”)。

see the answer here , thanks to Drew Dormann 这里看到答案,感谢Drew Dormann

template <class T, class C>
class proxy {
   friend C;

It worked for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM