[英]Partially specialized template friends
我有一个课程模板
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 >
成为class Bar< T >
的朋友。 这个的正确语法是什么?
谢谢!
在C ++ 03中是不可能的; C ++标准14.5.3 / 9说明如下:
朋友声明不得声明部分专业化。
如另一个答案所述,此问题可能有一些解决方法,但您要求的特定功能在该标准中不可用。
幸运的是,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;
};
没有C ++ 11,我认为你能做的最好的是假类型别名,可能需要一些代码(构造函数)重复(这可能无法解决你正在尝试的真正问题):
template< typename G, int N > class Foo { /* ... */ };
template<typename G> class FooAlias : public Foo<G, 0> { };
template< typename T >
class Bar {
template< typename G > friend class FooAlias;
/* ... */
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.