递归模板类

时间:2015-12-07 16:01:29

标签: c++11 metaprogramming template-meta-programming

我有两个类需要保持彼此的引用,如下所示:

template <typename S>
class H { 
public:
  H(const S& s): s_{s} {}
private:
  const S& s_;
}

然后

template <typename H>
class S { 
public:
  S(const H& h): h_{h} {}
private:
  const H& h_;
}

我无法定义任何类似

的内容
Service<ItsHandler<Service<....> 

有更好的主意吗?

1 个答案:

答案 0 :(得分:3)

抽象基类和多态如何?:

class baseHandler {
public:
    virtual func1() = 0;
    virtual ~baseHandler() = 0;
};

template <typename S>
class H : public baseHandler {
public:
  H(const S& s): s_{s} {}
private:
  const S& s_;
};

//template <typename H>
class S {
public:
  S(const baseHandler& h): h_{h} {}
private:
  const baseHandler& h_;
};