C++模板,部分专业化和朋友

C++ templates, partial specialization and friends

本文关键字:专业化 朋友 模板 C++      更新时间:2023-10-16

我很难让一个类与模板及其所有部分专业化成为朋友。有没有什么具体的技巧来实现这一点,或者有一些我不知道的限制?

class Y{
  template<class T>
  friend class X; // friends all instantiation forms of X
  void a_private_func() const{}
};
template<class T>
class X{
public:
  void f(Y const& y){ y.a_private_func(); }
};
template<class T>
class X<T*>{
public:
  void g(Y const& y){ y.a_private_func(); }
};

Ideone上的实例。