我有一个课程模板
template< typename G,int N > class Foo { /* ... */ };
我希望N = 0的特化是另一个类的朋友,但我不知道它的语法(我自己也找不到它).我试过了:
template< typename T > class Bar { template< typename G > friend class Foo< G,0 >; /* ... */ };
我想要任何类型G Foo< G,0>成为Bar<类的朋友T>.这个的正确语法是什么? 谢谢!
解决方法
在C 03中是不可能的; C标准14.5.3 / 9说明如下:
Friend declarations shall not declare partial specializations.
如另一个答案所述,此问题可能有一些解决方法,但您要求的特定功能在该标准中不可用.
幸运的是,C 11现在得到了很好的支持,并且能够指定模板别名,我们可以实现这一点:
template <typename,typename> struct X{}; template <typename T> struct Y { template <typename U> using X_partial = X<T,U>; template <typename> friend class X_partial; };