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

时间:2012-02-22 12:27:42

标签: c++ templates

我无法通过模板及其所有部分专业化来使课程成为朋友。是否有任何特定的技巧可以实现这一点或我不知道的一些限制?

1 个答案:

答案 0 :(得分:2)

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(); }
};

Live example on Ideone.